train_cmd.c
author glx
Mon, 24 Sep 2007 03:08:47 +0000
branch0.5
changeset 5545 f42dc59a45f5
parent 5528 b14613d2096f
permissions -rw-r--r--
(svn r11153) [0.5] -Fix [FS#1251]: incorrect usage of {G} tag in slovak translation
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1883
diff changeset
     4
#include "openttd.h"
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3191
diff changeset
     5
#include "bridge_map.h"
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
     6
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2150
diff changeset
     7
#include "functions.h"
2561
c78c3d248897 (svn r3098) static, const, uint -> TileIndex, indentation, bracing, unused return values, ... mostly related to the clone vehicle GUI
tron
parents: 2552
diff changeset
     8
#include "gui.h"
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
     9
#include "station_map.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 495
diff changeset
    10
#include "table/strings.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 593
diff changeset
    11
#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: 1208
diff changeset
    12
#include "tile.h"
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
    13
#include "tunnel_map.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "pathfind.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    17
#include "npf.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
#include "station.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#include "table/train_cmd.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#include "engine.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
#include "player.h"
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
    23
#include "sound.h"
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
    24
#include "depot.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1531
diff changeset
    25
#include "waypoint.h"
1752
cdbfb2f23e72 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1698
diff changeset
    26
#include "vehicle_gui.h"
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
    27
#include "train.h"
2982
286f69caaf05 (svn r3557) - NewGRF: move callback enums to a new file as more than just engines will use them
peter1138
parents: 2970
diff changeset
    28
#include "newgrf_callbacks.h"
2962
dbd168a4703a (svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents: 2952
diff changeset
    29
#include "newgrf_engine.h"
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
    30
#include "newgrf_sound.h"
3727
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
    31
#include "newgrf_text.h"
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
    32
#include "direction.h"
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
    33
#include "yapf/yapf.h"
4261
2ec8f5a9747b (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4252
diff changeset
    34
#include "date.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
    36
static bool TrainCheckIfLineEnds(Vehicle *v);
5252
6823da581c30 (svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents: 5215
diff changeset
    37
static void TrainController(Vehicle *v, bool update_image);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
    39
static const byte _vehicle_initial_x_fract[4] = {10, 8, 4,  8};
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
    40
static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
static const byte _state_dir_table[4] = { 0x20, 8, 0x10, 4 };
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
5163
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    43
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    44
/** Return the cargo weight multiplier to use for a rail vehicle
5316
b04421921eae (svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents: 5303
diff changeset
    45
 * @param cargo Cargo type to get multiplier for
5163
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    46
 * @return Cargo weight multiplier
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    47
 */
5316
b04421921eae (svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents: 5303
diff changeset
    48
byte FreightWagonMult(CargoID cargo)
5163
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    49
{
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    50
	// XXX NewCargos introduces a specific "is freight" flag for this test.
5316
b04421921eae (svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents: 5303
diff changeset
    51
	if (cargo == CT_PASSENGERS || cargo == CT_MAIL) return 1;
5163
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    52
	return _patches.freight_trains;
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    53
}
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    54
83acad83bbdd (svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents: 5162
diff changeset
    55
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    56
/**
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    57
 * Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    58
 * the consist changes.
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    59
 * @param v First vehicle of the consist.
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    60
 */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2704
diff changeset
    61
static void TrainCargoChanged(Vehicle* v)
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
    62
{
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    63
	Vehicle *u;
5162
f1d90a883266 (svn r7268) -Codechange: change rail vehicle cached weight from 16 to 32 bit values to avoid possible overflows.
peter1138
parents: 5116
diff changeset
    64
	uint32 weight = 0;
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    65
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    66
	for (u = v; u != NULL; u = u->next) {
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    67
		const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
5316
b04421921eae (svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents: 5303
diff changeset
    68
		uint32 vweight = (_cargoc.weights[u->cargo_type] * u->cargo_count * FreightWagonMult(u->cargo_type)) / 16;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    69
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    70
		// Vehicle weight is not added for articulated parts.
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
    71
		if (!IsArticulatedPart(u)) {
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    72
			// vehicle weight is the sum of the weight of the vehicle and the weight of its cargo
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    73
			vweight += rvi->weight;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    74
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    75
			// powered wagons have extra weight added
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    76
			if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON))
4017
035aa73fb8d0 (svn r5240) - NewGRF: when there is more than one engine in a consist, base livery overrides and powered wagon weight/power on the last engine before the wagon.
peter1138
parents: 3988
diff changeset
    77
				vweight += RailVehInfo(u->u.rail.first_engine)->pow_wag_weight;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    78
		}
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    79
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    80
		// consist weight is the sum of the weight of all vehicles in the consist
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    81
		weight += vweight;
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    82
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    83
		// store vehicle weight in cache
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    84
		u->u.rail.cached_veh_weight = vweight;
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    85
	};
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    86
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    87
	// store consist weight in cache
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    88
	v->u.rail.cached_weight = weight;
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    89
}
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    90
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    91
/**
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
    92
 * Recalculates the cached total power of a train. Should be called when the consist is changed
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
    93
 * @param v First vehicle of the consist.
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
    94
 */
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
    95
void TrainPowerChanged(Vehicle* v)
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
    96
{
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
    97
	Vehicle* u;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
    98
	uint32 power = 0;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
    99
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   100
	for (u = v; u != NULL; u = u->next) {
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   101
		const RailVehicleInfo *rvi_u;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   102
		bool engine_has_power = true;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   103
		bool wagon_has_power = true;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   104
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   105
		/* Power is not added for articulated parts */
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   106
		if (IsArticulatedPart(u)) continue;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   107
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
   108
		if (IsBridgeTile(u->tile) && IsBridgeMiddle(u->tile) && DiagDirToAxis(DirToDiagDir(u->direction)) == GetBridgeAxis(u->tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
   109
			if (!HasPowerOnRail(u->u.rail.railtype, GetRailTypeOnBridge(u->tile))) engine_has_power = false;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
   110
			if (!HasPowerOnRail(v->u.rail.railtype, GetRailTypeOnBridge(u->tile))) wagon_has_power = false;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
   111
		} else if (IsLevelCrossingTile(u->tile)) {
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
   112
			if (!HasPowerOnRail(u->u.rail.railtype, GetRailTypeCrossing(u->tile))) engine_has_power = false;
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
   113
			if (!HasPowerOnRail(v->u.rail.railtype, GetRailTypeCrossing(u->tile))) wagon_has_power = false;
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   114
		} else {
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   115
			if (!HasPowerOnRail(u->u.rail.railtype, GetRailType(u->tile))) engine_has_power = false;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   116
			if (!HasPowerOnRail(v->u.rail.railtype, GetRailType(u->tile))) wagon_has_power = false;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   117
		}
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   118
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   119
		rvi_u = RailVehInfo(u->engine_type);
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   120
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   121
		if (engine_has_power) power += rvi_u->power;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   122
		if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON) && (wagon_has_power)) {
4017
035aa73fb8d0 (svn r5240) - NewGRF: when there is more than one engine in a consist, base livery overrides and powered wagon weight/power on the last engine before the wagon.
peter1138
parents: 3988
diff changeset
   123
			power += RailVehInfo(u->u.rail.first_engine)->pow_wag_power;
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   124
		}
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   125
	}
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   126
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   127
	if (v->u.rail.cached_power != power) {
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   128
		v->u.rail.cached_power = power;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   129
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
4556
693f37c36c85 (svn r6395) -Fix: when converting a depot from/to elrail, update the power of trains that are in it
glx
parents: 4549
diff changeset
   130
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   131
	}
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   132
}
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   133
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   134
/**
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   135
 * Recalculates the cached stuff of a train. Should be called each time a vehicle is added
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   136
 * to/removed from the chain, and when the game is loaded.
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   137
 * Note: this needs to be called too for 'wagon chains' (in the depot, without an engine)
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   138
 * @param v First vehicle of the chain.
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   139
 */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   140
void TrainConsistChanged(Vehicle* v)
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   141
{
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   142
	const RailVehicleInfo *rvi_v;
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   143
	Vehicle *u;
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   144
	uint16 max_speed = 0xFFFF;
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   145
	EngineID first_engine;
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   146
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   147
	assert(v->type == VEH_Train);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   148
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   149
	assert(IsFrontEngine(v) || IsFreeWagon(v));
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   150
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   151
	rvi_v = RailVehInfo(v->engine_type);
2999
f5351c475765 (svn r3579) v->u.rail.first_engine holds an engine type, not a vehicle index, so use INVALID_ENGINE.
peter1138
parents: 2993
diff changeset
   152
	first_engine = IsFrontEngine(v) ? v->engine_type : INVALID_ENGINE;
2587
d554e2a03afa (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2574
diff changeset
   153
	v->u.rail.cached_total_length = 0;
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   154
	v->u.rail.compatible_railtypes = 0;
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   155
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   156
	for (u = v; u != NULL; u = u->next) {
1908
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   157
		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   158
		uint16 veh_len;
1908
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   159
2993
48b4308ed6a1 (svn r3570) - Explicitly update v->first in TrainConsistChanged() if necessary, as this is far faster than brute forcing it later.
peter1138
parents: 2989
diff changeset
   160
		// Update the v->first cache. This is faster than having to brute force it later.
48b4308ed6a1 (svn r3570) - Explicitly update v->first in TrainConsistChanged() if necessary, as this is far faster than brute forcing it later.
peter1138
parents: 2989
diff changeset
   161
		if (u->first == NULL) u->first = v;
48b4308ed6a1 (svn r3570) - Explicitly update v->first in TrainConsistChanged() if necessary, as this is far faster than brute forcing it later.
peter1138
parents: 2989
diff changeset
   162
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   163
		// update the 'first engine'
2999
f5351c475765 (svn r3579) v->u.rail.first_engine holds an engine type, not a vehicle index, so use INVALID_ENGINE.
peter1138
parents: 2993
diff changeset
   164
		u->u.rail.first_engine = (v == u) ? INVALID_ENGINE : first_engine;
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   165
		u->u.rail.railtype = GetEngine(u->engine_type)->railtype;
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   166
4017
035aa73fb8d0 (svn r5240) - NewGRF: when there is more than one engine in a consist, base livery overrides and powered wagon weight/power on the last engine before the wagon.
peter1138
parents: 3988
diff changeset
   167
		if (IsTrainEngine(u)) first_engine = u->engine_type;
035aa73fb8d0 (svn r5240) - NewGRF: when there is more than one engine in a consist, base livery overrides and powered wagon weight/power on the last engine before the wagon.
peter1138
parents: 3988
diff changeset
   168
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   169
		if (rvi_u->visual_effect != 0) {
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   170
			u->u.rail.cached_vis_effect = rvi_u->visual_effect;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   171
		} else {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   172
			if (IsTrainWagon(u) || IsArticulatedPart(u)) {
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   173
				// Wagons and articulated parts have no effect by default
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   174
				u->u.rail.cached_vis_effect = 0x40;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   175
			} else if (rvi_u->engclass == 0) {
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   176
				// Steam is offset by -4 units
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   177
				u->u.rail.cached_vis_effect = 4;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   178
			} else {
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   179
				// Diesel fumes and sparks come from the centre
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   180
				u->u.rail.cached_vis_effect = 8;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   181
			}
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   182
		}
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   183
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   184
		if (!IsArticulatedPart(u)) {
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   185
			// check if its a powered wagon
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   186
			CLRBIT(u->u.rail.flags, VRF_POWEREDWAGON);
3926
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   187
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   188
			/* Check powered wagon / visual effect callback */
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   189
			if (HASBIT(EngInfo(u->engine_type)->callbackmask, CBM_WAGON_POWER)) {
3926
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   190
				uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, u->engine_type, u);
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   191
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   192
				if (callback != CALLBACK_FAILED) u->u.rail.cached_vis_effect = callback;
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   193
			}
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   194
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   195
			if ((rvi_v->pow_wag_power != 0) && (rvi_u->flags & RVI_WAGON) && UsesWagonOverride(u)) {
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   196
				if (u->u.rail.cached_vis_effect < 0x40) {
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   197
					/* wagon is powered */
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   198
					SETBIT(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   199
				}
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   200
			}
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   201
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   202
			/* Do not count powered wagons for the compatible railtypes, as wagons always
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   203
			   have railtype normal */
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   204
			if (rvi_u->power > 0) {
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   205
				v->u.rail.compatible_railtypes |= GetRailTypeInfo(u->u.rail.railtype)->powered_railtypes;
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   206
			}
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   207
5116
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   208
			/* Some electric engines can be allowed to run on normal rail. It happens to all
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   209
			 * existing electric engines when elrails are disabled and then re-enabled */
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   210
			if (HASBIT(u->u.rail.flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL)) {
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   211
				u->u.rail.railtype = RAILTYPE_RAIL;
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   212
				u->u.rail.compatible_railtypes |= (1 << RAILTYPE_RAIL);
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   213
			}
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   214
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   215
			// max speed is the minimum of the speed limits of all vehicles in the consist
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   216
			if (!(rvi_u->flags & RVI_WAGON) || _patches.wagon_speed_limits)
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   217
				if (rvi_u->max_speed != 0 && !UsesWagonOverride(u))
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   218
					max_speed = min(rvi_u->max_speed, max_speed);
1908
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   219
		}
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   220
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   221
		// check the vehicle length (callback)
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   222
		veh_len = CALLBACK_FAILED;
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   223
		if (HASBIT(EngInfo(u->engine_type)->callbackmask, CBM_VEHICLE_LENGTH)) {
3390
f910bc27153e (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3355
diff changeset
   224
			veh_len = GetVehicleCallback(CBID_TRAIN_VEHICLE_LENGTH, 0, 0, u->engine_type, u);
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   225
		}
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   226
		if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
1934
b3d568113d4d (svn r2440) - Fix: [newgrf] Fix the spritesorter to handle overlapping sprites properly, this fixes display problems with really short wagons. (algorithm by patchman, ported by therax)
hackykid
parents: 1927
diff changeset
   227
		veh_len = clamp(veh_len, 0, u->next == NULL ? 7 : 5); // the clamp on vehicles not the last in chain is stricter, as too short wagons can break the 'follow next vehicle' code
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   228
		u->u.rail.cached_veh_length = 8 - veh_len;
2587
d554e2a03afa (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2574
diff changeset
   229
		v->u.rail.cached_total_length += u->u.rail.cached_veh_length;
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   230
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   231
	};
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   232
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   233
	// store consist weight/max speed in cache
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   234
	v->u.rail.cached_max_speed = max_speed;
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   235
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   236
	TrainPowerChanged(v);
1908
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   237
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   238
	// recalculate cached weights too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added)
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   239
	TrainCargoChanged(v);
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   240
}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   241
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   242
/* These two arrays are used for realistic acceleration. XXX: How should they
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   243
 * be interpreted? */
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   244
static const byte _curve_neighbours45[8][2] = {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   245
	{7, 1},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   246
	{0, 2},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   247
	{1, 3},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   248
	{2, 4},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   249
	{3, 5},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   250
	{4, 6},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   251
	{5, 7},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   252
	{6, 0},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   253
};
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   254
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   255
static const byte _curve_neighbours90[8][2] = {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   256
	{6, 2},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   257
	{7, 3},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   258
	{0, 4},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   259
	{1, 5},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   260
	{2, 6},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   261
	{3, 7},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   262
	{4, 0},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   263
	{5, 1},
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   264
};
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   265
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   266
enum AccelType {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   267
	AM_ACCEL,
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   268
	AM_BRAKE
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   269
};
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   270
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   271
static bool TrainShouldStop(const Vehicle* v, TileIndex tile)
1236
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   272
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   273
	const Order* o = &v->current_order;
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   274
	StationID sid = GetStationIndex(tile);
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   275
1236
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   276
	assert(v->type == VEH_Train);
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   277
	//When does a train drive through a station
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   278
	//first we deal with the "new nonstop handling"
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   279
	if (_patches.new_nonstop && o->flags & OF_NON_STOP && sid == o->dest) {
1236
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   280
		return false;
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   281
	}
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   282
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   283
	if (v->last_station_visited == sid) return false;
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   284
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   285
	if (sid != o->dest && (o->flags & OF_NON_STOP || _patches.new_nonstop)) {
1236
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   286
		return false;
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   287
	}
1236
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   288
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   289
	return true;
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   290
}
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   291
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   292
//new acceleration
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   293
static int GetTrainAcceleration(Vehicle *v, bool mode)
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   294
{
1531
4ee3c51e2b32 (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   295
	const Vehicle *u;
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
   296
	int num = 0; //number of vehicles, change this into the number of axles later
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   297
	int power = 0;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   298
	int mass = 0;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   299
	int max_speed = 2000;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   300
	int area = 120;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   301
	int friction = 35; //[1e-3]
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
   302
	int drag_coeff = 20; //[1e-4]
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   303
	int incl = 0;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   304
	int resistance;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   305
	int speed = v->cur_speed; //[mph]
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   306
	int force = 0x3FFFFFFF;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   307
	int pos = 0;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   308
	int lastpos = -1;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   309
	int curvecount[2] = {0, 0};
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   310
	int sum = 0;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   311
	int numcurve = 0;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   312
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   313
	speed *= 10;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   314
	speed /= 16;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   315
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   316
	//first find the curve speed limit
1531
4ee3c51e2b32 (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   317
	for (u = v; u->next != NULL; u = u->next, pos++) {
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
   318
		Direction dir = u->direction;
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
   319
		Direction ndir = u->next->direction;
1531
4ee3c51e2b32 (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   320
		int i;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   321
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   322
		for (i = 0; i < 2; i++) {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   323
			if ( _curve_neighbours45[dir][i] == ndir) {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   324
				curvecount[i]++;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   325
				if (lastpos != -1) {
1531
4ee3c51e2b32 (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   326
					numcurve++;
4ee3c51e2b32 (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   327
					sum += pos - lastpos;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   328
					if (pos - lastpos == 1) {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   329
						max_speed = 88;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   330
					}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   331
				}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   332
				lastpos = pos;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   333
			}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   334
		}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   335
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   336
		//if we have a 90 degree turn, fix the speed limit to 60
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   337
		if (_curve_neighbours90[dir][0] == ndir ||
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   338
				_curve_neighbours90[dir][1] == ndir) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   339
			max_speed = 61;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   340
		}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   341
	}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   342
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   343
	if (numcurve > 0) sum /= numcurve;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   344
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   345
	if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   346
		int total = curvecount[0] + curvecount[1];
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   347
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   348
		if (curvecount[0] == 1 && curvecount[1] == 1) {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   349
			max_speed = 0xFFFF;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   350
		} else if (total > 1) {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   351
			max_speed = 232 - (13 - clamp(sum, 1, 12)) * (13 - clamp(sum, 1, 12));
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   352
		}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   353
	}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   354
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   355
	max_speed += (max_speed / 2) * v->u.rail.railtype;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   356
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   357
	if (IsTileType(v->tile, MP_STATION) && IsFrontEngine(v)) {
1236
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   358
		if (TrainShouldStop(v, v->tile)) {
3928
791f04fb29ad (svn r5063) -Codechange: Add a function to determine the length of a platform (request by KUDr)
celestar
parents: 3926
diff changeset
   359
			uint station_length = GetPlatformLength(v->tile, DirToDiagDir(v->direction));
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   360
			int delta_v;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   361
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   362
			max_speed = 120;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   363
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   364
			delta_v = v->cur_speed / (station_length + 1);
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   365
			if (v->max_speed > (v->cur_speed - delta_v))
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   366
				max_speed = v->cur_speed - (delta_v / 10);
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   367
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   368
			max_speed = max(max_speed, 25 * station_length);
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   369
		}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   370
	}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   371
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   372
	mass = v->u.rail.cached_weight;
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   373
	power = v->u.rail.cached_power * 746;
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   374
	max_speed = min(max_speed, v->u.rail.cached_max_speed);
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   375
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   376
	for (u = v; u != NULL; u = u->next) {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   377
		num++;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   378
		drag_coeff += 3;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   379
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
   380
		if (u->u.rail.track == 0x80) max_speed = min(max_speed, 61);
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   381
1683
2248e6615517 (svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents: 1681
diff changeset
   382
		if (HASBIT(u->u.rail.flags, VRF_GOINGUP)) {
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
   383
			incl += u->u.rail.cached_veh_weight * 60; //3% slope, quite a bit actually
1683
2248e6615517 (svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents: 1681
diff changeset
   384
		} else if (HASBIT(u->u.rail.flags, VRF_GOINGDOWN)) {
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   385
			incl -= u->u.rail.cached_veh_weight * 60;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   386
		}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   387
	}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   388
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   389
	v->max_speed = max_speed;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   390
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   391
	if (v->u.rail.railtype != RAILTYPE_MAGLEV) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   392
		resistance = 13 * mass / 10;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   393
		resistance += 60 * num;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   394
		resistance += friction * mass * speed / 1000;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   395
		resistance += (area * drag_coeff * speed * speed) / 10000;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
   396
	} else {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   397
		resistance = (area * (drag_coeff / 2) * speed * speed) / 10000;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
   398
	}
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   399
	resistance += incl;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   400
	resistance *= 4; //[N]
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   401
5277
5d278741650c (svn r7421) -Fix (r2475): Changed "kick off" acceleration resulted in only a small amount of power being applied whilst moving off and then double the power at 1 mph. This resulted in a perceived delay before trains moved. Fix this by applying the full power of the engine (or the kick off, whichever is greater). Essay over.
peter1138
parents: 5259
diff changeset
   402
	/* Due to the mph to m/s conversion below, at speeds below 3 mph the force is
5d278741650c (svn r7421) -Fix (r2475): Changed "kick off" acceleration resulted in only a small amount of power being applied whilst moving off and then double the power at 1 mph. This resulted in a perceived delay before trains moved. Fix this by applying the full power of the engine (or the kick off, whichever is greater). Essay over.
peter1138
parents: 5259
diff changeset
   403
	 * actually double the train's power */
5d278741650c (svn r7421) -Fix (r2475): Changed "kick off" acceleration resulted in only a small amount of power being applied whilst moving off and then double the power at 1 mph. This resulted in a perceived delay before trains moved. Fix this by applying the full power of the engine (or the kick off, whichever is greater). Essay over.
peter1138
parents: 5259
diff changeset
   404
	if (speed > 2) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   405
		switch (v->u.rail.railtype) {
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   406
			case RAILTYPE_RAIL:
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
   407
			case RAILTYPE_ELECTRIC:
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   408
			case RAILTYPE_MONO:
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   409
				force = power / speed; //[N]
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   410
				force *= 22;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   411
				force /= 10;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   412
				break;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   413
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   414
			case RAILTYPE_MAGLEV:
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   415
				force = power / 25;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   416
				break;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   417
		}
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   418
	} else {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   419
		//"kickoff" acceleration
5277
5d278741650c (svn r7421) -Fix (r2475): Changed "kick off" acceleration resulted in only a small amount of power being applied whilst moving off and then double the power at 1 mph. This resulted in a perceived delay before trains moved. Fix this by applying the full power of the engine (or the kick off, whichever is greater). Essay over.
peter1138
parents: 5259
diff changeset
   420
		force = max(power, (mass * 8) + resistance);
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   421
	}
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   422
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   423
	if (force <= 0) force = 10000;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   424
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   425
	if (v->u.rail.railtype != RAILTYPE_MAGLEV) force = min(force, mass * 10 * 200);
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   426
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   427
	if (mode == AM_ACCEL) {
1684
d09bc0beec4c (svn r2188) Revert r2185, because trains always accerlated (Requested by Celestar)
tron
parents: 1683
diff changeset
   428
		return (force - resistance) / (mass * 4);
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   429
	} else {
5279
658a16e02195 (svn r7425) -Fix (r1681): With realistic acceleration, guarantee a minimum braking
peter1138
parents: 5277
diff changeset
   430
		return min((-force - resistance) / (mass * 4), -10000 / (mass * 4));
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   431
	}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   432
}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   433
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2805
diff changeset
   434
static void UpdateTrainAcceleration(Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
{
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   436
	uint power = 0;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   437
	uint weight = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   439
	assert(IsFrontEngine(v));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   441
	weight = v->u.rail.cached_weight;
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   442
	power = v->u.rail.cached_power;
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   443
	v->max_speed = v->u.rail.cached_max_speed;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
	assert(weight != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   447
	v->acceleration = clamp(power / weight * 4, 1, 255);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   449
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
   450
int GetTrainImage(const Vehicle* v, Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
	int img = v->spritenum;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
	int base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
   455
	if (HASBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
   456
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
	if (is_custom_sprite(img)) {
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   458
		base = GetCustomVehicleSprite(v, direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img));
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   459
		if (base != 0) return base;
2464
3dee59d241c0 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2463
diff changeset
   460
		img = orig_rail_vehicle_info[v->engine_type].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   462
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
	base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   465
	if (v->cargo_count >= v->cargo_cap / 2) base += _wagon_full_adder[img];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
	return base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   469
void DrawTrainEngine(int x, int y, EngineID engine, uint32 image_ormod)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
{
540
fbed599dadbb (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   471
	const RailVehicleInfo *rvi = RailVehInfo(engine);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   472
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   473
	int img = rvi->image_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
	uint32 image = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
	if (is_custom_sprite(img)) {
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
   477
		image = GetCustomVehicleIcon(engine, DIR_W);
2570
c7c4d8438516 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   478
		if (image == 0) {
c7c4d8438516 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   479
			img = orig_rail_vehicle_info[engine].image_index;
c7c4d8438516 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   480
		} else {
c7c4d8438516 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   481
			y += _traininfo_vehicle_pitch;
c7c4d8438516 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   482
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
	}
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   484
	if (image == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
		image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   488
	if (rvi->flags & RVI_MULTIHEAD) {
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   489
		DrawSprite(image | image_ormod, x - 14, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
		x += 15;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
		image = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
		if (is_custom_sprite(img)) {
358
6eb0d5ec99b1 (svn r546) -newgrf: Use GetCustomVehicleIcon() for fetching sprites for vehicle selection dialogs. (Idea by octo, done by pasky).
darkvater
parents: 337
diff changeset
   493
			image = GetCustomVehicleIcon(engine, 2);
2464
3dee59d241c0 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2463
diff changeset
   494
			if (image == 0) img = orig_rail_vehicle_info[engine].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
		}
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   496
		if (image == 0) {
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   497
			image =
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   498
				((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) +
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   499
				_engine_sprite_base[img + 1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   502
	DrawSprite(image | image_ormod, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   504
4737
e38af7736681 (svn r6649) - Codechange: Show more correct capacity of articulated wagons in the train purchase list.
peter1138
parents: 4725
diff changeset
   505
uint CountArticulatedParts(EngineID engine_type)
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   506
{
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   507
	uint16 callback;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   508
	uint i;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   509
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   510
	if (!HASBIT(EngInfo(engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return 0;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   511
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   512
	for (i = 1; i < 10; i++) {
3390
f910bc27153e (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3355
diff changeset
   513
		callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, engine_type, NULL);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   514
		if (callback == CALLBACK_FAILED || callback == 0xFF) break;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   515
	}
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   516
2608
f7f41be75b48 (svn r3145) Fix two off-by-one errors in building articulated rail vehicles.
peter1138
parents: 2607
diff changeset
   517
	return i - 1;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   518
}
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   519
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   520
static void AddArticulatedParts(Vehicle **vl)
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   521
{
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   522
	const RailVehicleInfo *rvi_artic;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   523
	EngineID engine_type;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   524
	Vehicle *v = vl[0];
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   525
	Vehicle *u = v;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   526
	uint16 callback;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   527
	bool flip_image;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   528
	uint i;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   529
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   530
	if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   531
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   532
	for (i = 1; i < 10; i++) {
4831
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   533
		callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, v->engine_type, v);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
   534
		if (callback == CALLBACK_FAILED || callback == 0xFF) return;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   535
4831
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   536
		/* Attempt to use pre-allocated vehicles until they run out. This can happen
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   537
		 * if the callback returns different values depending on the cargo type. */
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   538
		u->next = vl[i];
4831
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   539
		if (u->next == NULL) u->next = AllocateVehicle();
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   540
		if (u->next == NULL) return;
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   541
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   542
		u = u->next;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   543
2608
f7f41be75b48 (svn r3145) Fix two off-by-one errors in building articulated rail vehicles.
peter1138
parents: 2607
diff changeset
   544
		engine_type = GB(callback, 0, 7);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   545
		flip_image = HASBIT(callback, 7);
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   546
		rvi_artic = RailVehInfo(engine_type);
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   547
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   548
		// get common values from first engine
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   549
		u->direction = v->direction;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   550
		u->owner = v->owner;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   551
		u->tile = v->tile;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   552
		u->x_pos = v->x_pos;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   553
		u->y_pos = v->y_pos;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   554
		u->z_pos = v->z_pos;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   555
		u->z_height = v->z_height;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   556
		u->u.rail.track = v->u.rail.track;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   557
		u->u.rail.railtype = v->u.rail.railtype;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   558
		u->build_year = v->build_year;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   559
		u->vehstatus = v->vehstatus & ~VS_STOPPED;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   560
		u->u.rail.first_engine = v->engine_type;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   561
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   562
		// get more settings from rail vehicle info
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   563
		u->spritenum = rvi_artic->image_index;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   564
		if (flip_image) u->spritenum++;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   565
		u->cargo_type = rvi_artic->cargo_type;
3870
766d7bd6c299 (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3836
diff changeset
   566
		u->cargo_subtype = 0;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   567
		u->cargo_cap = rvi_artic->capacity;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   568
		u->max_speed = 0;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   569
		u->max_age = 0;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   570
		u->engine_type = engine_type;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   571
		u->value = 0;
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   572
		u->type = VEH_Train;
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   573
		u->subtype = 0;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   574
		SetArticulatedPart(u);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   575
		u->cur_image = 0xAC2;
2804
b932fe148d36 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2758
diff changeset
   576
		u->random_bits = VehicleRandomBits();
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   577
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   578
		VehiclePositionChanged(u);
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   579
	}
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   580
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   582
static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
	int32 value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
	const RailVehicleInfo *rvi;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   586
	uint num_vehicles;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
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: 882
diff changeset
   588
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
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: 882
diff changeset
   589
540
fbed599dadbb (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   590
	rvi = RailVehInfo(engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
	value = (rvi->base_cost * _price.build_railwagon) >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   593
	num_vehicles = 1 + CountArticulatedParts(engine);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   594
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
	if (!(flags & DC_QUERY_COST)) {
2609
b2c7aa8ce0c4 (svn r3146) Avoid use of variable size declared arrays when allocating articulated rail vehicles.
peter1138
parents: 2608
diff changeset
   596
		Vehicle *vl[11]; // Allow for wagon and upto 10 artic parts.
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   597
		Vehicle* v;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   598
		int x;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   599
		int y;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   600
4831
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   601
		memset(&vl, 0, sizeof(vl));
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   602
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   603
		if (!AllocateVehicles(vl, num_vehicles))
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   604
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
		if (flags & DC_EXEC) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   607
			Vehicle *u, *w;
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   608
			DiagDirection dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   610
			v = vl[0];
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   611
			v->spritenum = rvi->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   612
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   613
			u = NULL;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   614
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   615
			FOR_ALL_VEHICLES(w) {
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   616
				if (w->type == VEH_Train && w->tile == tile &&
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   617
				    IsFreeWagon(w) && w->engine_type == engine) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   618
					u = GetLastVehicleInChain(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
			v->engine_type = engine;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
3185
c0ef161f700f (svn r3831) Add and use GetRailDepotDirection()
tron
parents: 3184
diff changeset
   625
			dir = GetRailDepotDirection(tile);
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   626
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   627
			v->direction = DiagDirToDir(dir);
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   628
			v->tile = tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   629
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   630
			x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   631
			y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
			v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
			v->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
			v->z_pos = GetSlopeZ(x,y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
			v->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
			v->z_height = 6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
			v->u.rail.track = 0x80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
			v->vehstatus = VS_HIDDEN | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   641
			v->subtype = 0;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   642
			SetTrainWagon(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
			if (u != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
				u->next = v;
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   645
			} else {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   646
				SetFreeWagon(v);
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
   647
				InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
			v->cargo_type = rvi->cargo_type;
3870
766d7bd6c299 (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3836
diff changeset
   651
			v->cargo_subtype = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
			v->cargo_cap = rvi->capacity;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
			v->value = value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
//			v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   656
			v->u.rail.railtype = GetEngine(engine)->railtype;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   657
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   658
			v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
			v->type = VEH_Train;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
			v->cur_image = 0xAC2;
2804
b932fe148d36 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2758
diff changeset
   661
			v->random_bits = VehicleRandomBits();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   662
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   663
			AddArticulatedParts(vl);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   664
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   665
			_new_vehicle_id = v->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
			VehiclePositionChanged(v);
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   668
			TrainConsistChanged(GetFirstVehicleInChain(v));
4574
497540b4a75f (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4559
diff changeset
   669
			GetPlayer(_current_player)->num_engines[engine]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   670
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   671
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
2970
77fa46c313c0 (svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents: 2962
diff changeset
   672
			if (IsLocalPlayer()) {
77fa46c313c0 (svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents: 2962
diff changeset
   673
				InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window
77fa46c313c0 (svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents: 2962
diff changeset
   674
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   675
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   676
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   677
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   678
	return value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
}
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
// Move all free vehicles in the depot to the train
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   682
static void NormalizeTrainVehInDepot(const Vehicle* u)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   684
	const Vehicle* v;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   685
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
	FOR_ALL_VEHICLES(v) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   687
		if (v->type == VEH_Train && IsFreeWagon(v) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   688
				v->tile == u->tile &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
				v->u.rail.track == 0x80) {
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   690
			if (CmdFailed(DoCommand(0, v->index | (u->index << 16), 1, DC_EXEC,
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   691
					CMD_MOVE_RAIL_VEHICLE)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   693
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   697
static int32 EstimateTrainCost(const RailVehicleInfo* rvi)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   698
{
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
   699
	return rvi->base_cost * (_price.build_railvehicle >> 3) >> 5;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2805
diff changeset
   702
static void AddRearEngineToMultiheadedTrain(Vehicle* v, Vehicle* u, bool building)
1060
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   703
{
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   704
	u->direction = v->direction;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   705
	u->owner = v->owner;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   706
	u->tile = v->tile;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   707
	u->x_pos = v->x_pos;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   708
	u->y_pos = v->y_pos;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   709
	u->z_pos = v->z_pos;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   710
	u->z_height = 6;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   711
	u->u.rail.track = 0x80;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   712
	u->vehstatus = v->vehstatus & ~VS_STOPPED;
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   713
	u->subtype = 0;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   714
	SetMultiheaded(u);
1060
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   715
	u->spritenum = v->spritenum + 1;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   716
	u->cargo_type = v->cargo_type;
3870
766d7bd6c299 (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3836
diff changeset
   717
	u->cargo_subtype = v->cargo_subtype;
1060
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   718
	u->cargo_cap = v->cargo_cap;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   719
	u->u.rail.railtype = v->u.rail.railtype;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   720
	if (building) v->next = u;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   721
	u->engine_type = v->engine_type;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   722
	u->build_year = v->build_year;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   723
	if (building) v->value >>= 1;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   724
	u->value = v->value;
1060
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   725
	u->type = VEH_Train;
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   726
	u->cur_image = 0xAC2;
2804
b932fe148d36 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2758
diff changeset
   727
	u->random_bits = VehicleRandomBits();
1060
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   728
	VehiclePositionChanged(u);
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   729
}
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   730
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   731
/** Build a railroad vehicle.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   732
 * @param tile tile of the depot where rail-vehicle is built
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   733
 * @param p1 engine type id
3816
2cdb29b57c1f (svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents: 3815
diff changeset
   734
 * @param p2 bit 0 when set, the train will get number 0, otherwise it will get a free number
2cdb29b57c1f (svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents: 3815
diff changeset
   735
 *           bit 1 prevents any free cars from being added to the train
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   737
int32 CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
	const RailVehicleInfo *rvi;
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   740
	int value;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   741
	Vehicle *v;
1282
e7a73ee62d2f (svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents: 1266
diff changeset
   742
	UnitID unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   743
	Engine *e;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   744
	uint num_vehicles;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   746
	/* Check if the engine-type is valid (for the player) */
5215
439d5ca71311 (svn r7330) -Fix (r7304): Data invalidation doesn't always happen as the local
peter1138
parents: 5211
diff changeset
   747
	if (!IsEngineBuildable(p1, VEH_Train, _current_player)) return_cmd_error(STR_ENGINE_NOT_BUILDABLE);
1196
115f46e3807d (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1192
diff changeset
   748
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   749
	/* Check if the train is actually being built in a depot belonging
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   750
	 * to the player. Doesn't matter if only the cost is queried */
1443
d9c101f7634b (svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents: 1438
diff changeset
   751
	if (!(flags & DC_QUERY_COST)) {
d9c101f7634b (svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents: 1438
diff changeset
   752
		if (!IsTileDepotType(tile, TRANSPORT_RAIL)) return CMD_ERROR;
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: 1895
diff changeset
   753
		if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
1443
d9c101f7634b (svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents: 1438
diff changeset
   754
	}
1221
2b159ebb4899 (svn r1725) - Fix: now trains can only be built in depots and you have to own the depot too (hacked client protection)
bjarni
parents: 1214
diff changeset
   755
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   756
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   757
540
fbed599dadbb (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   758
	rvi = RailVehInfo(p1);
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   759
	e = GetEngine(p1);
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   760
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   761
	/* Check if depot and new engine uses the same kind of tracks */
3411
0275a29a3f88 (svn r4226) -Fix: [elrails] it's no longer possible to build electric engines in non-electric depots
bjarni
parents: 3390
diff changeset
   762
	/* We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */
0275a29a3f88 (svn r4226) -Fix: [elrails] it's no longer possible to build electric engines in non-electric depots
bjarni
parents: 3390
diff changeset
   763
	if (!HasPowerOnRail(e->railtype, GetRailType(tile))) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   764
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   765
	if (rvi->flags & RVI_WAGON) return CmdBuildRailWagon(p1, tile, flags);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   766
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   767
	value = EstimateTrainCost(rvi);
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2244
diff changeset
   768
2677
2c1fcffb304c (svn r3219) -Codechange: removed the now obsolite code in the build train command to make half multiheaded engines
bjarni
parents: 2676
diff changeset
   769
	num_vehicles = (rvi->flags & RVI_MULTIHEAD) ? 2 : 1;
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   770
	num_vehicles += CountArticulatedParts(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   771
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   772
	if (!(flags & DC_QUERY_COST)) {
2609
b2c7aa8ce0c4 (svn r3146) Avoid use of variable size declared arrays when allocating articulated rail vehicles.
peter1138
parents: 2608
diff changeset
   773
		Vehicle *vl[12]; // Allow for upto 10 artic parts and dual-heads
4831
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   774
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   775
		memset(&vl, 0, sizeof(vl));
327d181a60ee (svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents: 4806
diff changeset
   776
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   777
		if (!AllocateVehicles(vl, num_vehicles) || IsOrderPoolFull())
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   778
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   779
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   780
		v = vl[0];
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   781
4095
55a5642143f1 (svn r5427) - Codechange: testing for "== true" is redundant.
peter1138
parents: 4017
diff changeset
   782
		unit_num = HASBIT(p2, 0) ? 0 : GetFreeUnitNumber(VEH_Train);
3816
2cdb29b57c1f (svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents: 3815
diff changeset
   783
		if (unit_num > _patches.max_trains)
2cdb29b57c1f (svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents: 3815
diff changeset
   784
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
2cdb29b57c1f (svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents: 3815
diff changeset
   785
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
		if (flags & DC_EXEC) {
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   787
			DiagDirection dir = GetRailDepotDirection(tile);
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   788
			int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir];
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   789
			int y = TileY(tile) * TILE_SIZE + _vehicle_initial_y_fract[dir];
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   790
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
			v->unitnumber = unit_num;
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   792
			v->direction = DiagDirToDir(dir);
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   793
			v->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
			v->owner = _current_player;
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   795
			v->x_pos = x;
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   796
			v->y_pos = y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
			v->z_pos = GetSlopeZ(x,y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
			v->z_height = 6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
			v->u.rail.track = 0x80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
			v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
			v->spritenum = rvi->image_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
			v->cargo_type = rvi->cargo_type;
3870
766d7bd6c299 (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3836
diff changeset
   803
			v->cargo_subtype = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
			v->cargo_cap = rvi->capacity;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
			v->max_speed = rvi->max_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
			v->value = value;
1266
9dc5638fe8cc (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   807
			v->last_station_visited = INVALID_STATION;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   808
			v->dest_tile = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   809
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   810
			v->engine_type = p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   812
			v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   813
			v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   814
			v->max_age = e->lifelength * 366;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   815
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
			v->string_id = STR_SV_TRAIN_NAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
			v->u.rail.railtype = e->railtype;
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   818
			_new_vehicle_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   819
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
			v->service_interval = _patches.servint_trains;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
			v->date_of_last_service = _date;
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   822
			v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   823
			v->type = VEH_Train;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   824
			v->cur_image = 0xAC2;
2804
b932fe148d36 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2758
diff changeset
   825
			v->random_bits = VehicleRandomBits();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   827
			v->subtype = 0;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   828
			SetFrontEngine(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   829
			SetTrainEngine(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   830
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
			VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
2677
2c1fcffb304c (svn r3219) -Codechange: removed the now obsolite code in the build train command to make half multiheaded engines
bjarni
parents: 2676
diff changeset
   833
			if (rvi->flags & RVI_MULTIHEAD) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   834
				SetMultiheaded(v);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   835
				AddRearEngineToMultiheadedTrain(vl[0], vl[1], true);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   836
				/* Now we need to link the front and rear engines together
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   837
				 * other_multiheaded_part is the pointer that links to the other half of the engine
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   838
				 * vl[0] is the front and vl[1] is the rear
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   839
				 */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   840
				vl[0]->u.rail.other_multiheaded_part = vl[1];
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   841
				vl[1]->u.rail.other_multiheaded_part = vl[0];
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   842
			} else {
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
   843
				AddArticulatedParts(vl);
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   844
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   846
			TrainConsistChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
			UpdateTrainAcceleration(v);
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   848
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
   849
			if (!HASBIT(p2, 1)) { // check if the cars should be added to the new vehicle
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   850
				NormalizeTrainVehInDepot(v);
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   851
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
4574
497540b4a75f (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4559
diff changeset
   853
			GetPlayer(_current_player)->num_engines[p1]++;
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
   854
			InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   855
			RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   856
			InvalidateWindow(WC_COMPANY, v->owner);
2618
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
   857
			if (IsLocalPlayer()) {
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
   858
				InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
   859
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
	}
1128
d3ffc98b92ad (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
   862
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   863
	return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   867
/* Check if all the wagons of the given train are in a depot, returns the
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
   868
 * number of cars (including loco) then. If not it returns -1 */
4648
ab94e3a447a8 (svn r6524) -Code cleanup r6515: cleaned up the command to start/stop all vehicles in a depot.
bjarni
parents: 4632
diff changeset
   869
int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   871
	int count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
	TileIndex tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   873
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   874
	/* check if stopped in a depot */
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
   875
	if (!IsTileDepotType(tile, TRANSPORT_RAIL) || v->cur_speed != 0) return -1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
	count = 0;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   878
	for (; v != NULL; v = v->next) {
2844
7a55ad0a6a36 (svn r3392) Train depot fixes:
peter1138
parents: 2840
diff changeset
   879
		/* This count is used by the depot code to determine the number of engines
7a55ad0a6a36 (svn r3392) Train depot fixes:
peter1138
parents: 2840
diff changeset
   880
		 * in the consist. Exclude articulated parts so that autoreplacing to
3986
fdffc0b284cf (svn r5176) fixing the typo, that should have been fixed in last commit
bjarni
parents: 3985
diff changeset
   881
		 * engines with more articulated parts than before works correctly.
3985
87cf4955787d (svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents: 3977
diff changeset
   882
		 *
87cf4955787d (svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents: 3977
diff changeset
   883
		 * Also skip counting rear ends of multiheaded engines */
87cf4955787d (svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents: 3977
diff changeset
   884
		if (!IsArticulatedPart(v) && !(!IsTrainEngine(v) && IsMultiheaded(v))) count++;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   885
		if (v->u.rail.track != 0x80 || v->tile != tile ||
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   886
				(IsFrontEngine(v) && needs_to_be_stopped && !(v->vehstatus & VS_STOPPED))) {
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   887
			return -1;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   888
		}
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   889
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
	return count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   894
/* Used to check if the train is inside the depot and verifying that the VS_STOPPED flag is set */
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   895
inline int CheckTrainStoppedInDepot(const Vehicle *v)
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   896
{
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   897
	return CheckTrainInDepot(v, true);
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   898
}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   899
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   900
/* Used to check if the train is inside the depot, but not checking the VS_STOPPED flag */
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   901
inline bool CheckTrainIsInsideDepot(const Vehicle *v)
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   902
{
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   903
	return (CheckTrainInDepot(v, false) > 0);
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   904
}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   905
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   906
/**
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   907
 * Unlink a rail wagon from the consist.
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   908
 * @param v Vehicle to remove.
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   909
 * @param first The first vehicle of the consist.
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   910
 * @return The first vehicle of the consist.
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   911
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
{
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   914
	Vehicle *u;
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   915
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   916
	// unlinking the first vehicle of the chain?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   917
	if (v == first) {
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   918
		v = GetNextVehicle(v);
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   919
		if (v == NULL) return NULL;
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   920
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   921
		if (IsTrainWagon(v)) SetFreeWagon(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   922
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
		return v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
	}
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   925
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   926
	for (u = first; GetNextVehicle(u) != v; u = GetNextVehicle(u)) {}
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   927
	GetLastEnginePart(u)->next = GetNextVehicle(v);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   928
	return first;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   930
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   931
static Vehicle *FindGoodVehiclePos(const Vehicle *src)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
	Vehicle *dst;
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   934
	EngineID eng = src->engine_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
	TileIndex tile = src->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
	FOR_ALL_VEHICLES(dst) {
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
   938
		if (dst->type == VEH_Train && IsFreeWagon(dst) && dst->tile == tile) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
			// check so all vehicles in the line have the same engine.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
			Vehicle *v = dst;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   941
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
			while (v->engine_type == eng) {
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   943
				v = v->next;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   944
				if (v == NULL) return dst;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   952
/*
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   953
 * add a vehicle v behind vehicle dest
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   954
 * use this function since it sets flags as needed
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   955
 */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   956
static void AddWagonToConsist(Vehicle *v, Vehicle *dest)
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   957
{
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   958
	UnlinkWagon(v, GetFirstVehicleInChain(v));
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   959
	if (dest == NULL) return;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   960
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   961
	v->next = dest->next;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   962
	dest->next = v;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   963
	ClearFreeWagon(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   964
	ClearFrontEngine(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   965
}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   966
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   967
/*
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   968
 * move around on the train so rear engines are placed correctly according to the other engines
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   969
 * always call with the front engine
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   970
 */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   971
static void NormaliseTrainConsist(Vehicle *v)
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   972
{
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   973
	Vehicle *u;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   974
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   975
	if (IsFreeWagon(v)) return;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   976
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   977
	assert(IsFrontEngine(v));
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   978
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   979
	for (; v != NULL; v = GetNextVehicle(v)) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   980
		if (!IsMultiheaded(v) || !IsTrainEngine(v)) continue;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   981
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   982
		/* make sure that there are no free cars before next engine */
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   983
		for (u = v; u->next != NULL && !IsTrainEngine(u->next); u = u->next);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   984
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   985
		if (u == v->u.rail.other_multiheaded_part) continue;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   986
		AddWagonToConsist(v->u.rail.other_multiheaded_part, u);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   987
	}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   988
}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   989
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   990
/** Move a rail vehicle around inside the depot.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   991
 * @param tile unused
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   992
 * @param p1 various bitstuffed elements
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   993
 * - p1 (bit  0 - 15) source vehicle index
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   994
 * - p1 (bit 16 - 31) what wagon to put the source wagon AFTER, XXX - INVALID_VEHICLE to make a new line
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   995
 * @param p2 (bit 0) move all vehicles following the source vehicle
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
   997
int32 CmdMoveRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
{
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
   999
	VehicleID s = GB(p1, 0, 16);
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1000
	VehicleID d = GB(p1, 16, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
	Vehicle *src, *dst, *src_head, *dst_head;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1003
	if (!IsValidVehicleID(s)) return CMD_ERROR;
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1004
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1005
	src = GetVehicle(s);
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1006
5447
fdd232ac18d0 (svn r8883) [0.5] -Backport from trunk (r8740, r8793, r8865, r8878):
Darkvater
parents: 5316
diff changeset
  1007
	if (src->type != VEH_Train || !CheckOwnership(src->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
	// if nothing is selected as destination, try and find a matching vehicle to drag to.
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1010
	if (d == INVALID_VEHICLE) {
4198
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1011
		dst = IsTrainEngine(src) ? NULL : FindGoodVehiclePos(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
	} else {
5447
fdd232ac18d0 (svn r8883) [0.5] -Backport from trunk (r8740, r8793, r8865, r8878):
Darkvater
parents: 5316
diff changeset
  1013
		if (!IsValidVehicleID(d)) return CMD_ERROR;
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1014
		dst = GetVehicle(d);
5447
fdd232ac18d0 (svn r8883) [0.5] -Backport from trunk (r8740, r8793, r8865, r8878):
Darkvater
parents: 5316
diff changeset
  1015
		if (dst->type != VEH_Train || !CheckOwnership(dst->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1017
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1018
	// if an articulated part is being handled, deal with its parent vehicle
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1019
	while (IsArticulatedPart(src)) src = GetPrevVehicleInChain(src);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1020
	if (dst != NULL) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1021
		while (IsArticulatedPart(dst)) dst = GetPrevVehicleInChain(dst);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1022
	}
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1023
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
	// don't move the same vehicle..
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1025
	if (src == dst) return 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1026
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1027
	/* locate the head of the two chains */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
	src_head = GetFirstVehicleInChain(src);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1029
	if (dst != NULL) {
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1030
		dst_head = GetFirstVehicleInChain(dst);
5447
fdd232ac18d0 (svn r8883) [0.5] -Backport from trunk (r8740, r8793, r8865, r8878):
Darkvater
parents: 5316
diff changeset
  1031
		if (dst_head->tile != src_head->tile) return CMD_ERROR;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1032
		// Now deal with articulated part of destination wagon
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1033
		dst = GetLastEnginePart(dst);
5447
fdd232ac18d0 (svn r8883) [0.5] -Backport from trunk (r8740, r8793, r8865, r8878):
Darkvater
parents: 5316
diff changeset
  1034
	} else {
fdd232ac18d0 (svn r8883) [0.5] -Backport from trunk (r8740, r8793, r8865, r8878):
Darkvater
parents: 5316
diff changeset
  1035
		dst_head = NULL;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1036
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1037
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1038
	if (dst != NULL && IsMultiheaded(dst) && !IsTrainEngine(dst) && IsTrainWagon(src)) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1039
		/* We are moving a wagon to the rear part of a multiheaded engine */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1040
		if (dst->next == NULL) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1041
			/* It's the last one, so we will add the wagon just before the rear engine */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1042
			dst = GetPrevVehicleInChain(dst);
2825
f81c0b099d69 (svn r3373) -Fix: don't link a wagon to itself, which resulted in a wagon disapearing, and a depot which was unable to remove
truelight
parents: 2819
diff changeset
  1043
			/* Now if the vehicle we want to link to is the vehicle itself, drop out */
f81c0b099d69 (svn r3373) -Fix: don't link a wagon to itself, which resulted in a wagon disapearing, and a depot which was unable to remove
truelight
parents: 2819
diff changeset
  1044
			if (dst == src) return CMD_ERROR;
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1045
			// if dst is NULL, it means that dst got a rear multiheaded engine as first engine. We can't use that
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1046
			if (dst == NULL) return CMD_ERROR;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1047
		} else {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1048
			/* there are more units on this train, so we will add the wagon after the next one*/
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1049
			dst = dst->next;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1050
		}
1601
9fd461d00287 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
  1051
	}
9fd461d00287 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
  1052
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1053
	if (IsTrainEngine(src) && dst_head != NULL) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1054
		/* we need to make sure that we didn't place it between a pair of multiheaded engines */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1055
		Vehicle *u, *engine = NULL;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1056
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1057
		for (u = dst_head; u != NULL; u = u->next) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1058
			if (IsTrainEngine(u) && IsMultiheaded(u) && u->u.rail.other_multiheaded_part != NULL) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1059
				engine = u;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1060
			}
4198
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1061
			if (engine != NULL && engine->u.rail.other_multiheaded_part == u) {
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1062
				engine = NULL;
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1063
			}
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1064
			if (u == dst) {
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1065
				if (engine != NULL) dst = engine->u.rail.other_multiheaded_part;
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1066
				break;
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1067
			}
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1068
		}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1069
	}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1070
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1071
	if (IsMultiheaded(src) && !IsTrainEngine(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1072
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
	// when moving all wagons, we can't have the same src_head and dst_head
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1074
	if (HASBIT(p2, 0) && src_head == dst_head) return 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1075
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1076
	{
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1077
		int src_len = 0;
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1078
		int max_len = _patches.mammoth_trains ? 100 : 9;
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1079
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1080
		// check if all vehicles in the source train are stopped inside a depot.
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1081
		src_len = CheckTrainStoppedInDepot(src_head);
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
  1082
		if (src_len < 0) return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED);
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1083
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1084
		// check the destination row if the source and destination aren't the same.
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1085
		if (src_head != dst_head) {
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1086
			int dst_len = 0;
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1087
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1088
			if (dst_head != NULL) {
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1089
				// check if all vehicles in the dest train are stopped.
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1090
				dst_len = CheckTrainStoppedInDepot(dst_head);
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
  1091
				if (dst_len < 0) return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED);
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1092
			}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1093
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1094
			// We are moving between rows, so only count the wagons from the source
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1095
			// row that are being moved.
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1096
			if (HASBIT(p2, 0)) {
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1097
				const Vehicle *u;
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1098
				for (u = src_head; u != src && u != NULL; u = GetNextVehicle(u))
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1099
					src_len--;
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1100
			} else {
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1101
				// If moving only one vehicle, just count that.
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1102
				src_len = 1;
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1103
			}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1104
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1105
			if (src_len + dst_len > max_len) {
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1106
				// Abort if we're adding too many wagons to a train.
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1107
				if (dst_head != NULL && IsFrontEngine(dst_head)) return_cmd_error(STR_8819_TRAIN_TOO_LONG);
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1108
				// Abort if we're making a train on a new row.
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1109
				if (dst_head == NULL && IsTrainEngine(src)) return_cmd_error(STR_8819_TRAIN_TOO_LONG);
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1110
			}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1111
		} else {
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1112
			// Abort if we're creating a new train on an existing row.
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1113
			if (src_len > max_len && src == src_head && IsTrainEngine(GetNextVehicle(src_head)))
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1114
				return_cmd_error(STR_8819_TRAIN_TOO_LONG);
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1115
		}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1116
	}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1117
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
	// moving a loco to a new line?, then we need to assign a unitnumber.
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1119
	if (dst == NULL && !IsFrontEngine(src) && IsTrainEngine(src)) {
1282
e7a73ee62d2f (svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents: 1266
diff changeset
  1120
		UnitID unit_num = GetFreeUnitNumber(VEH_Train);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1121
		if (unit_num > _patches.max_trains)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1124
		if (flags & DC_EXEC) src->unitnumber = unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
3727
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1127
	if (dst_head != NULL) {
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1128
		/* Check NewGRF Callback 0x1D */
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1129
		uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, dst_head->engine_type, src, dst_head);
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1130
		if (callback != CALLBACK_FAILED) {
3729
c4ef8b67998f (svn r4706) - NewGRF: add the correct default error message for the wagon attach callback.
peter1138
parents: 3727
diff changeset
  1131
			if (callback == 0xFD) return_cmd_error(STR_INCOMPATIBLE_RAIL_TYPES);
3727
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1132
			if (callback < 0xFD) {
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1133
				StringID error = GetGRFStringID(GetEngineGRFID(dst_head->engine_type), 0xD000 + callback);
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1134
				return_cmd_error(error);
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1135
			}
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1136
		}
b4d9a8ab4ce4 (svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents: 3657
diff changeset
  1137
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
	/* do it? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
	if (flags & DC_EXEC) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1141
		/* clear the ->first cache */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1142
		{
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1143
			Vehicle *u;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1144
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1145
			for (u = src_head; u != NULL; u = u->next) u->first = NULL;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1146
			for (u = dst_head; u != NULL; u = u->next) u->first = NULL;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1147
		}
2607
08f42ae6fbfc (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1148
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1149
		if (HASBIT(p2, 0)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
			// unlink ALL wagons
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
			if (src != src_head) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
				Vehicle *v = src_head;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1153
				while (GetNextVehicle(v) != src) v = GetNextVehicle(v);
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1154
				GetLastEnginePart(v)->next = NULL;
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1155
			} else {
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1156
				InvalidateWindowData(WC_VEHICLE_DEPOT, src_head->tile); // We removed a line
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1157
				src_head = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1158
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
		} else {
1921
263f1bcfc038 (svn r2427) - Fix: CmdMoveRailVehice; Prevent possible assertion failure when moving vehicles within one chain.
hackykid
parents: 1917
diff changeset
  1160
			// if moving within the same chain, dont use dst_head as it may get invalidated
4198
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1161
			if (src_head == dst_head) dst_head = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1162
			// unlink single wagon from linked list
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1163
			src_head = UnlinkWagon(src, src_head);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1164
			GetLastEnginePart(src)->next = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1165
		}
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
		if (dst == NULL) {
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1168
			/* We make a new line in the depot, so we know already that we invalidate the window data */
4806
a75776d5c5a4 (svn r6728) -Fix r6651: solved issue where moving rail vehicles in a depot could read from a NULL pointer
bjarni
parents: 4739
diff changeset
  1169
			InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1170
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1171
			// move the train to an empty line. for locomotives, we set the type to TS_Front. for wagons, 4.
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1172
			if (IsTrainEngine(src)) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1173
				if (!IsFrontEngine(src)) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1174
					// setting the type to 0 also involves setting up the orders field.
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1175
					SetFrontEngine(src);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1176
					assert(src->orders == NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1177
					src->num_orders = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1179
			} else {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1180
				SetFreeWagon(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
			}
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1182
			dst_head = src;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1183
		} else {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1184
			if (IsFrontEngine(src)) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1185
				// the vehicle was previously a loco. need to free the order list and delete vehicle windows etc.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1186
				DeleteWindowById(WC_VEHICLE_VIEW, src->index);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1187
				DeleteVehicleOrders(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1188
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1189
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1190
			if (IsFrontEngine(src) || IsFreeWagon(src)) {
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1191
				InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1192
				ClearFrontEngine(src);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1193
				ClearFreeWagon(src);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1194
				src->unitnumber = 0; // doesn't occupy a unitnumber anymore.
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1195
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1196
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1197
			// link in the wagon(s) in the chain.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1198
			{
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1199
				Vehicle *v;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1200
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1201
				for (v = src; GetNextVehicle(v) != NULL; v = GetNextVehicle(v));
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1202
				GetLastEnginePart(v)->next = dst->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1203
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1204
			dst->next = src;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1205
		}
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1206
		if (src->u.rail.other_multiheaded_part != NULL) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1207
			if (src->u.rail.other_multiheaded_part == src_head) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1208
				src_head = src_head->next;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1209
			}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1210
			AddWagonToConsist(src->u.rail.other_multiheaded_part, src);
4197
84acfde415e0 (svn r5664) -Fix: [SF 1518090 ] moving train engines in depot - crash (svn r5463)
bjarni
parents: 4182
diff changeset
  1211
			// previous line set the front engine to the old front. We need to clear that
84acfde415e0 (svn r5664) -Fix: [SF 1518090 ] moving train engines in depot - crash (svn r5463)
bjarni
parents: 4182
diff changeset
  1212
			src->u.rail.other_multiheaded_part->first = NULL;
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1213
		}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1214
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1215
		if (HASBIT(p2, 0) && src_head != NULL && src_head != src) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1216
			/* if we stole a rear multiheaded engine, we better give it back to the front end */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1217
			Vehicle *engine = NULL, *u;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1218
			for (u = src_head; u != NULL; u = u->next) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1219
				if (IsMultiheaded(u)) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1220
					if (IsTrainEngine(u)) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1221
						engine = u;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1222
						continue;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1223
					}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1224
					/* we got the rear engine to match with the front one */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1225
					engine = NULL;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1226
				}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1227
			}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1228
			if (engine != NULL && engine->u.rail.other_multiheaded_part != NULL) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1229
				AddWagonToConsist(engine->u.rail.other_multiheaded_part, engine);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1230
				// previous line set the front engine to the old front. We need to clear that
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1231
				engine->u.rail.other_multiheaded_part->first = NULL;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1232
			}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1233
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1234
2607
08f42ae6fbfc (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1235
		/* If there is an engine behind first_engine we moved away, it should become new first_engine
08f42ae6fbfc (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1236
		 * To do this, CmdMoveRailVehicle must be called once more
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1237
		 * we can't loop forever here because next time we reach this line we will have a front engine */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1238
		if (src_head != NULL && !IsFrontEngine(src_head) && IsTrainEngine(src_head)) {
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1239
			CmdMoveRailVehicle(0, flags, src_head->index | (INVALID_VEHICLE << 16), 1);
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  1240
			src_head = NULL; // don't do anything more to this train since the new call will do it
2607
08f42ae6fbfc (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1241
		}
08f42ae6fbfc (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1242
4198
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1243
		if (src_head != NULL) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1244
			NormaliseTrainConsist(src_head);
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1245
			TrainConsistChanged(src_head);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1246
			if (IsFrontEngine(src_head)) {
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1247
				UpdateTrainAcceleration(src_head);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1248
				InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1249
				/* Update the refit button and window */
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1250
				InvalidateWindow(WC_VEHICLE_REFIT, src_head->index);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1251
				InvalidateWindowWidget(WC_VEHICLE_VIEW, src_head->index, 12);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1252
			}
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1253
			/* Update the depot window */
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1254
			InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1255
		};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1256
4198
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1257
		if (dst_head != NULL) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1258
			NormaliseTrainConsist(dst_head);
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1259
			TrainConsistChanged(dst_head);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1260
			if (IsFrontEngine(dst_head)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
				UpdateTrainAcceleration(dst_head);
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1262
				InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1263
				/* Update the refit button and window */
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1264
				InvalidateWindowWidget(WC_VEHICLE_VIEW, dst_head->index, 12);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1265
				InvalidateWindow(WC_VEHICLE_REFIT, dst_head->index);
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1266
			}
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1267
			/* Update the depot window */
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1268
			InvalidateWindow(WC_VEHICLE_DEPOT, dst_head->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1269
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1270
737
ceb3dce50a37 (svn r1189) Fix vehicle list update glitch when moving waggons in depots
tron
parents: 715
diff changeset
  1271
		RebuildVehicleLists();
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1275
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1277
/** Start/Stop a train.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1278
 * @param tile unused
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1279
 * @param p1 train to start/stop
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1280
 * @param p2 unused
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1281
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1282
int32 CmdStartStopTrain(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1283
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1284
	Vehicle *v;
4244
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1285
	uint16 callback;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1287
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1288
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1289
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1291
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1292
4244
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1293
	/* Check if this train can be started/stopped. The callback will fail or
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1294
	 * return 0xFF if it can. */
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1295
	callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1296
	if (callback != CALLBACK_FAILED && callback != 0xFF) {
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1297
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1298
		return_cmd_error(error);
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1299
	}
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1300
4251
85befcd5f435 (svn r5853) -Fix: [elrails] FS#178 Electric Trains can leave Conventional Depot
bjarni
parents: 4244
diff changeset
  1301
	if (v->vehstatus & VS_STOPPED && v->u.rail.cached_power == 0) return_cmd_error(STR_TRAIN_START_NO_CATENARY);
85befcd5f435 (svn r5853) -Fix: [elrails] FS#178 Electric Trains can leave Conventional Depot
bjarni
parents: 4244
diff changeset
  1302
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1303
	if (flags & DC_EXEC) {
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3022
diff changeset
  1304
		if (v->vehstatus & VS_STOPPED && v->u.rail.track == 0x80) {
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3022
diff changeset
  1305
			DeleteVehicleNews(p1, STR_8814_TRAIN_IS_WAITING_IN_DEPOT);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3022
diff changeset
  1306
		}
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3022
diff changeset
  1307
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1308
		v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1309
		v->vehstatus ^= VS_STOPPED;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  1310
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1311
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
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
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1314
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1315
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1316
/** Sell a (single) train wagon/engine.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1317
 * @param tile unused
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1318
 * @param p1 the wagon/engine index
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1319
 * @param p2 the selling mode
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1320
 * - p2 = 0: only sell the single dragged wagon/engine (and any belonging rear-engines)
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1321
 * - p2 = 1: sell the vehicle and all vehicles following it in the chain
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1322
             if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1323
 * - p2 = 2: when selling attached locos, rearrange all vehicles after it to separate lines;
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1324
 *           all wagons of the same type will go on the same line. Used by the AI currently
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1325
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1326
int32 CmdSellRailWagon(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
{
5509
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1328
	Window *w = NULL;
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1329
	Vehicle *v, *tmp, *first;
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1330
	Vehicle *new_f = NULL;
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1331
	int32 cost = 0;
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1332
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1333
	if (!IsValidVehicleID(p1) || p2 > 2) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1334
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1335
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1336
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1337
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1338
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1339
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1340
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1341
	while (IsArticulatedPart(v)) v = GetPrevVehicleInChain(v);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1342
	first = GetFirstVehicleInChain(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1343
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1344
	// make sure the vehicle is stopped in the depot
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
  1345
	if (CheckTrainStoppedInDepot(first) < 0) {
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
  1346
		return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED);
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
  1347
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1348
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1349
	if (IsMultiheaded(v) && !IsTrainEngine(v)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1350
1842
7efaede8ac8e (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1351
	if (flags & DC_EXEC) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1352
		if (v == first && IsFrontEngine(first)) {
5509
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1353
			w = FindWindowById(WC_VEHICLE_VIEW, first->index);
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1354
			if (w != NULL) DeleteWindow(w);
2618
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
  1355
		}
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
  1356
		if (IsLocalPlayer() && (p1 == 1 || !(RailVehInfo(v->engine_type)->flags & RVI_WAGON))) {
1842
7efaede8ac8e (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1357
			InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train);
7efaede8ac8e (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1358
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1359
		InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
1779
598bf208ad44 (svn r2283) - Fix (regression): [ 1197493 ] train_gui.c:1341: failed assertion ..., somehow RebuildVehicleList() got lost during the rewrite :P
Darkvater
parents: 1777
diff changeset
  1360
		RebuildVehicleLists();
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1361
	}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1362
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1363
	switch (p2) {
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1364
		case 0: case 2: { /* Delete given wagon */
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1365
			bool switch_engine = false;    // update second wagon to engine?
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1366
			byte ori_subtype = v->subtype; // backup subtype of deleted wagon in case DeleteVehicle() changes
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1367
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1368
			/* 1. Delete the engine, if it is dualheaded also delete the matching
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1369
			 * rear engine of the loco (from the point of deletion onwards) */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1370
			Vehicle *rear = (IsMultiheaded(v) &&
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1371
				IsTrainEngine(v)) ? v->u.rail.other_multiheaded_part : NULL;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1372
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1373
			if (rear != NULL) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1374
				cost -= rear->value;
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1375
				if (flags & DC_EXEC) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1376
					UnlinkWagon(rear, first);
5256
90226d8da8fb (svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents: 5252
diff changeset
  1377
					DeleteDepotHighlightOfVehicle(rear);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1378
					DeleteVehicle(rear);
0
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
			}
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1381
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1382
			/* 2. We are selling the first engine, some special action might be required
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1383
			 * here, so take attention */
1770
b6bda4dc6da2 (svn r2274) - Codechange: some comments, parentheses and EngineID typedef for engine_type
Darkvater
parents: 1766
diff changeset
  1384
			if ((flags & DC_EXEC) && v == first) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1385
				new_f = GetNextVehicle(first);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1386
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1387
				/* 2.1 If the first wagon is sold, update the first-> pointers to NULL */
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1388
				for (tmp = first; tmp != NULL; tmp = tmp->next) tmp->first = NULL;
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1389
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1390
				/* 2.2 If there are wagons present after the deleted front engine, check
5463
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1391
				 * if the second wagon (which will be first) is an engine. If it is one,
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1392
				 * promote it as a new train, retaining the unitnumber, orders */
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1393
				if (new_f != NULL && IsTrainEngine(new_f)) {
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1394
					switch_engine = true;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1395
					/* Copy important data from the front engine */
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1396
					new_f->unitnumber      = first->unitnumber;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1397
					new_f->current_order   = first->current_order;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1398
					new_f->cur_order_index = first->cur_order_index;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1399
					new_f->orders          = first->orders;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1400
					new_f->num_orders      = first->num_orders;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1401
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1402
					if (first->prev_shared != NULL) {
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1403
						first->prev_shared->next_shared = new_f;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1404
						new_f->prev_shared = first->prev_shared;
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1405
					}
5463
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1406
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1407
					if (first->next_shared != NULL) {
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1408
						first->next_shared->prev_shared = new_f;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1409
						new_f->next_shared = first->next_shared;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1410
					}
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1411
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1412
					/*
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1413
					 * Remove all order information from the front train, to
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1414
					 * prevent the order and the shared order list to be
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1415
					 * destroyed by Destroy/DeleteVehicle.
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1416
					 */
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1417
					first->orders      = NULL;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1418
					first->prev_shared = NULL;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1419
					first->next_shared = NULL;
eaf87cf4d276 (svn r9345) [0.5] -Backport from trunk (r9277, r9337, r9338):
rubidium
parents: 5447
diff changeset
  1420
5509
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1421
					/* If we deleted a window then open a new one for the 'new' train */
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1422
					if (IsLocalPlayer() && w != NULL) ShowTrainViewWindow(new_f);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1423
				}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1424
			}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1425
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1426
			/* 3. Delete the requested wagon */
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1427
			cost -= v->value;
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1428
			if (flags & DC_EXEC) {
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1429
				first = UnlinkWagon(v, first);
5256
90226d8da8fb (svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents: 5252
diff changeset
  1430
				DeleteDepotHighlightOfVehicle(v);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1431
				DeleteVehicle(v);
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1432
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1433
				/* 4 If the second wagon was an engine, update it to front_engine
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1434
					* which UnlinkWagon() has changed to TS_Free_Car */
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1435
				if (switch_engine) SetFrontEngine(first);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1436
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1437
				/* 5. If the train still exists, update its acceleration, window, etc. */
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1438
				if (first != NULL) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1439
					NormaliseTrainConsist(first);
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1440
					TrainConsistChanged(first);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1441
					if (IsFrontEngine(first)) {
1917
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1442
						InvalidateWindow(WC_VEHICLE_DETAILS, first->index);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1443
						InvalidateWindow(WC_VEHICLE_REFIT, first->index);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1444
						UpdateTrainAcceleration(first);
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1445
					}
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1446
				}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1447
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1448
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1449
				/* (6.) Borked AI. If it sells an engine it expects all wagons lined
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1450
				 * up on a new line to be added to the newly built loco. Replace it is.
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1451
				 * Totally braindead cause building a new engine adds all loco-less
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1452
				 * engines to its train anyways */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1453
				if (p2 == 2 && HASBIT(ori_subtype, Train_Front)) {
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1454
					for (v = first; v != NULL; v = tmp) {
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1455
						tmp = GetNextVehicle(v);
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1456
						DoCommand(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1457
					}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1458
				}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1459
			}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1460
		} break;
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1461
		case 1: { /* Delete wagon and all wagons after it given certain criteria */
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1462
			/* Start deleting every vehicle after the selected one
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1463
			 * If we encounter a matching rear-engine to a front-engine
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1464
			 * earlier in the chain (before deletion), leave it alone */
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1465
			for (; v != NULL; v = tmp) {
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1466
				tmp = GetNextVehicle(v);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1467
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1468
				if (IsMultiheaded(v)) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1469
					if (IsTrainEngine(v)) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1470
						/* We got a front engine of a multiheaded set. Now we will sell the rear end too */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1471
						Vehicle *rear = v->u.rail.other_multiheaded_part;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1472
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1473
						if (rear != NULL) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1474
							cost -= rear->value;
5509
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1475
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1476
							/* If this is a multiheaded vehicle with nothing
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1477
							 * between the parts, tmp will be pointing to the
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1478
							 * rear part, which is unlinked from the train and
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1479
							 * deleted here. However, because tmp has already
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1480
							 * been set it needs to be updated now so that the
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1481
							 * loop never sees the rear part. */
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1482
							if (tmp == rear) tmp = GetNextVehicle(tmp);
0b111f4e6dc9 (svn r10274) [0.5] -Backport from trunk (r9961, r10023, r10028, r10030, r10038, r10147):
rubidium
parents: 5467
diff changeset
  1483
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1484
							if (flags & DC_EXEC) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1485
								first = UnlinkWagon(rear, first);
5256
90226d8da8fb (svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents: 5252
diff changeset
  1486
								DeleteDepotHighlightOfVehicle(rear);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1487
								DeleteVehicle(rear);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1488
							}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1489
						}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1490
					} else if (v->u.rail.other_multiheaded_part != NULL) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1491
						/* The front to this engine is earlier in this train. Do nothing */
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1492
						continue;
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1493
					}
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1494
				}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1495
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1496
				cost -= v->value;
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1497
				if (flags & DC_EXEC) {
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1498
					first = UnlinkWagon(v, first);
5256
90226d8da8fb (svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents: 5252
diff changeset
  1499
					DeleteDepotHighlightOfVehicle(v);
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1500
					DeleteVehicle(v);
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1501
				}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1502
			}
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1503
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1504
			/* 3. If it is still a valid train after selling, update its acceleration and cached values */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1505
			if (flags & DC_EXEC && first != NULL) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1506
				NormaliseTrainConsist(first);
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1507
				TrainConsistChanged(first);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1508
				if (IsFrontEngine(first)) UpdateTrainAcceleration(first);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1509
				InvalidateWindow(WC_VEHICLE_DETAILS, first->index);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1510
				InvalidateWindow(WC_VEHICLE_REFIT, first->index);
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1511
			}
1766
7c048e309f18 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1512
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1514
	return cost;
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
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  1517
static void UpdateTrainDeltaXY(Vehicle *v, Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1518
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1519
#define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1520
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1521
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1523
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1525
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1527
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
		MKIT(7, 3, -3, -1),
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
#undef MKIT
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
	uint32 x = _delta_xy_table[direction];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1533
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  1534
	v->x_offs        = GB(x,  0, 8);
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  1535
	v->y_offs        = GB(x,  8, 8);
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  1536
	v->sprite_width  = GB(x, 16, 8);
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  1537
	v->sprite_height = GB(x, 24, 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1539
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1540
static void UpdateVarsAfterSwap(Vehicle *v)
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
	UpdateTrainDeltaXY(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1543
	v->cur_image = GetTrainImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1544
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1545
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1548
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1549
static void SetLastSpeed(Vehicle* v, int spd)
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1550
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
	int old = v->u.rail.last_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
	if (spd != old) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1553
		v->u.rail.last_speed = spd;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1554
		if (_patches.vehicle_speed || (old == 0) != (spd == 0))
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  1555
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1556
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1558
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1559
static void SwapTrainFlags(byte *swap_flag1, byte *swap_flag2)
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1560
{
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1561
	byte flag1, flag2;
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1562
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1563
	flag1 = *swap_flag1;
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1564
	flag2 = *swap_flag2;
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1565
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1566
	/* Clear the flags */
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1567
	CLRBIT(*swap_flag1, VRF_GOINGUP);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1568
	CLRBIT(*swap_flag1, VRF_GOINGDOWN);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1569
	CLRBIT(*swap_flag2, VRF_GOINGUP);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1570
	CLRBIT(*swap_flag2, VRF_GOINGDOWN);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1571
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1572
	/* Reverse the rail-flags (if needed) */
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1573
	if (HASBIT(flag1, VRF_GOINGUP)) {
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1574
		SETBIT(*swap_flag2, VRF_GOINGDOWN);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1575
	} else if (HASBIT(flag1, VRF_GOINGDOWN)) {
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1576
		SETBIT(*swap_flag2, VRF_GOINGUP);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1577
	}
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1578
	if (HASBIT(flag2, VRF_GOINGUP)) {
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1579
		SETBIT(*swap_flag1, VRF_GOINGDOWN);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1580
	} else if (HASBIT(flag2, VRF_GOINGDOWN)) {
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1581
		SETBIT(*swap_flag1, VRF_GOINGUP);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1582
	}
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1583
}
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1584
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1585
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1586
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1587
	Vehicle *a, *b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1588
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
	/* locate vehicles to swap */
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1590
	for (a = v; l != 0; l--) a = a->next;
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1591
	for (b = v; r != 0; r--) b = b->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1592
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1593
	if (a != b) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1594
		/* swap the hidden bits */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1595
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1596
			uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus&VS_HIDDEN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1597
			b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus&VS_HIDDEN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1598
			a->vehstatus = tmp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1599
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1600
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1601
		/* swap variables */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1602
		swap_byte(&a->u.rail.track, &b->u.rail.track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1603
		swap_byte(&a->direction, &b->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1604
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1605
		/* toggle direction */
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  1606
		if (!(a->u.rail.track & 0x80)) a->direction = ReverseDir(a->direction);
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  1607
		if (!(b->u.rail.track & 0x80)) b->direction = ReverseDir(b->direction);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1608
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1609
		/* swap more variables */
1174
27e386195965 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1151
diff changeset
  1610
		swap_int32(&a->x_pos, &b->x_pos);
27e386195965 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1151
diff changeset
  1611
		swap_int32(&a->y_pos, &b->y_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1612
		swap_tile(&a->tile, &b->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1613
		swap_byte(&a->z_pos, &b->z_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1614
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1615
		SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1616
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1617
		/* update other vars */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1618
		UpdateVarsAfterSwap(a);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1619
		UpdateVarsAfterSwap(b);
1554
c0b2287254f4 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1620
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  1621
		VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  1622
		VehicleEnterTile(b, b->tile, b->x_pos, b->y_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1623
	} else {
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  1624
		if (!(a->u.rail.track & 0x80)) a->direction = ReverseDir(a->direction);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1625
		UpdateVarsAfterSwap(a);
1554
c0b2287254f4 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1626
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  1627
		VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
	}
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  1629
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  1630
	/* Update train's power incase tiles were different rail type */
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  1631
	TrainPowerChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1632
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1633
744
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1634
/* Check if the vehicle is a train and is on the tile we are testing */
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1635
static void *TestTrainOnCrossing(Vehicle *v, void *data)
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1636
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1637
	if (v->tile != *(const TileIndex*)data || v->type != VEH_Train) return NULL;
744
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1638
	return v;
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1639
}
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1640
1103
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1641
static void DisableTrainCrossing(TileIndex tile)
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1642
{
3560
4389ae5ad967 (svn r4435) - Fix: an assertion triggered when trying to remove a bridge with the remove-tool (r4348 surfaced this). In CmdRemoveRoad tiletype was not checked for ownership. Intorudce IsLevelCrossingTile() which checks if a tile is a crossing without knowing the type. Suggested by peter1138 and Tron.
Darkvater
parents: 3546
diff changeset
  1643
	if (IsLevelCrossingTile(tile) &&
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1644
			VehicleFromPos(tile, &tile, TestTrainOnCrossing) == NULL && // empty?
3322
fa5d46929db9 (svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents: 3315
diff changeset
  1645
			IsCrossingBarred(tile)) {
fa5d46929db9 (svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents: 3315
diff changeset
  1646
		UnbarCrossing(tile);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1647
		MarkTileDirtyByTile(tile);
1103
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1648
	}
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1649
}
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1650
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1651
/**
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1652
 * Advances wagons for train reversing, needed for variable length wagons.
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1653
 * Needs to be called once before the train is reversed, and once after it.
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1654
 * @param v First vehicle in chain
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1655
 * @param before Set to true for the call before reversing, false otherwise
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1656
 */
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1657
static void AdvanceWagons(Vehicle *v, bool before)
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1658
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1659
	Vehicle* base;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1660
	Vehicle* first;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1661
	int length;
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1662
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1663
	base = v;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1664
	first = base->next;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1665
	length = CountVehiclesInChain(v);
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1666
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1667
	while (length > 2) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1668
		Vehicle* last;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1669
		int differential;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1670
		int i;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1671
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1672
		// find pairwise matching wagon
4432
33631ac88c40 (svn r6195) -Codechange: do not use '//' in '/* ... */' style comments and '/*' or '*/' in '//' style comments.
rubidium
parents: 4412
diff changeset
  1673
		// start<>end, start+1<>end-1, ...
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1674
		last = first;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1675
		for (i = length - 3; i > 0; i--) last = last->next;
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1676
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1677
		differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1678
		if (before) differential *= -1;
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1679
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1680
		if (differential > 0) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1681
			Vehicle* tempnext;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1682
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1683
			// disconnect last car to make sure only this subset moves
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1684
			tempnext = last->next;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1685
			last->next = NULL;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1686
5252
6823da581c30 (svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents: 5215
diff changeset
  1687
			for (i = 0; i < differential; i++) TrainController(first, false);
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1688
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1689
			last->next = tempnext;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1690
		}
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1691
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1692
		base = first;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1693
		first = first->next;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1694
		length -= 2;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1695
	}
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1696
}
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1697
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2928
diff changeset
  1698
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1699
static void ReverseTrainDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1700
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1701
	int l = 0, r = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1702
	Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1703
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1704
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) {
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1705
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1706
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1707
743
8161509f1864 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1708
	/* Check if we were approaching a rail/road-crossing */
8161509f1864 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1709
	{
8161509f1864 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1710
		TileIndex tile = v->tile;
3163
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1711
		DiagDirection dir = DirToDiagDir(v->direction);
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1712
1459
6c1f01803928 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1455
diff changeset
  1713
		/* Determine the diagonal direction in which we will exit this tile */
3163
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1714
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) {
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1715
			dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT);
743
8161509f1864 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1716
		}
8161509f1864 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1717
		/* Calculate next tile */
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4556
diff changeset
  1718
		tile += TileOffsByDiagDir(dir);
1103
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1719
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1720
		/* Check if the train left a rail/road-crossing */
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1721
		DisableTrainCrossing(tile);
743
8161509f1864 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1722
	}
8161509f1864 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1723
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1724
	// count number of vehicles
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1725
	u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1726
	do r++; while ( (u = u->next) != NULL );
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1727
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1728
	AdvanceWagons(v, true);
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1729
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1730
	/* swap start<>end, start+1<>end-1, ... */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1731
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1732
		ReverseTrainSwapVeh(v, l++, r--);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1733
	} while (l <= r);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1734
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1735
	AdvanceWagons(v, false);
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1736
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1737
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) {
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1738
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1739
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1740
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1741
	CLRBIT(v->u.rail.flags, VRF_REVERSING);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1742
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1743
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1744
/** Reverse train.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1745
 * @param tile unused
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1746
 * @param p1 train to reverse
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1747
 * @param p2 if true, reverse a unit in a train (needs to be in a depot)
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1748
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1749
int32 CmdReverseTrainDirection(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1750
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1751
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1752
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1753
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1754
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1755
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1756
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1757
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1758
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1759
	if (p2) {
3257
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1760
		// turn a single unit around
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1761
		Vehicle *front;
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1762
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
  1763
		if (IsMultiheaded(v) || HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) {
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1764
			return_cmd_error(STR_ONLY_TURN_SINGLE_UNIT);
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1765
		}
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1766
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1767
		front = GetFirstVehicleInChain(v);
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1768
		// make sure the vehicle is stopped in the depot
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1769
		if (CheckTrainStoppedInDepot(front) < 0) {
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1770
			return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED);
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1771
		}
3257
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1772
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1773
		if (flags & DC_EXEC) {
3259
e4992824d715 (svn r3947) use TOGGLEBIT() instead of manual bit toggling in CmdReverseTrainDirection (pointed out by glx)
bjarni
parents: 3257
diff changeset
  1774
			TOGGLEBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION);
4856
3ed01482b9de (svn r6782) - Fix (r3947): Invalidate depot & vehicle windows when reversing a single engine with ctrl-click.
peter1138
parents: 4839
diff changeset
  1775
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
3ed01482b9de (svn r6782) - Fix (r3947): Invalidate depot & vehicle windows when reversing a single engine with ctrl-click.
peter1138
parents: 4839
diff changeset
  1776
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
3257
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1777
		}
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1778
	} else {
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1779
		//turn the whole train around
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1780
		if (v->u.rail.crash_anim_pos != 0 || v->breakdown_ctr != 0) return CMD_ERROR;
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1781
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1782
		if (flags & DC_EXEC) {
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1783
			if (_patches.realistic_acceleration && v->cur_speed != 0) {
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1784
				TOGGLEBIT(v->u.rail.flags, VRF_REVERSING);
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1785
			} else {
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1786
				v->cur_speed = 0;
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1787
				SetLastSpeed(v, 0);
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1788
				ReverseTrainDirection(v);
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1789
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1790
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1791
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1792
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1793
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1794
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1795
/** Force a train through a red signal
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1796
 * @param tile unused
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1797
 * @param p1 train to ignore the red signal
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1798
 * @param p2 unused
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1799
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1800
int32 CmdForceTrainProceed(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
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
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1803
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1804
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1805
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1806
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1807
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1808
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1809
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1810
	if (flags & DC_EXEC) v->u.rail.force_proceed = 0x50;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1811
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1812
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1813
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1814
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1815
/** Refits a train to the specified cargo type.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1816
 * @param tile unused
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1817
 * @param p1 vehicle ID of the train to refit
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1818
 * param p2 various bitstuffed elements
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1819
 * - p2 = (bit 0-7) - the new cargo type to refit to
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1820
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1821
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1822
int32 CmdRefitRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1823
{
2635
88b8b74c01ac (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2634
diff changeset
  1824
	CargoID new_cid = GB(p2, 0, 8);
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1825
	byte new_subtype = GB(p2, 8, 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1826
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1827
	int32 cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1828
	uint num;
842
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1829
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1830
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1831
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1832
	v = GetVehicle(p1);
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1833
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1834
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
  1835
	if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1836
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1837
	/* Check cargo */
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1838
	if (new_cid > NUM_CARGO) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1839
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1840
	SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1841
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1842
	cost = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1843
	num = 0;
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1844
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1845
	do {
491
0642006d876c (svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents: 445
diff changeset
  1846
		/* XXX: We also refit all the attached wagons en-masse if they
0642006d876c (svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents: 445
diff changeset
  1847
		 * can be refitted. This is how TTDPatch does it.  TODO: Have
0642006d876c (svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents: 445
diff changeset
  1848
		 * some nice [Refit] button near each wagon. --pasky */
2704
dc9ae68dfd74 (svn r3248) - Codechange: Change interface of CanRefitTo() to supply the engine type directly instead of getting it from a vehicle. This allows the function to be used before vehicles are involved.
peter1138
parents: 2677
diff changeset
  1849
		if (!CanRefitTo(v->engine_type, new_cid)) continue;
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1850
1859
b3af0081df39 (svn r2365) - Change: [refitting] Make refitting capacities for trains newgrf compatible. Train vehicles can now carry twice as much mail/goods as other cargo, and four times as much passengers.
hackykid
parents: 1842
diff changeset
  1851
		if (v->cargo_cap != 0) {
2463
de2605ed3758 (svn r2989) - Make engine/vehicle information tables constant. Duplicate them so NewGRF data can be loaded without wiping out the default data.
peter1138
parents: 2456
diff changeset
  1852
			const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
1895
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1853
			uint16 amount = CALLBACK_FAILED;
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1854
3956
0f43adbc293a (svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents: 3955
diff changeset
  1855
			if (HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_REFIT_CAPACITY)) {
3988
b76291f6ce29 (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  1856
				/* Back up the vehicle's cargo type */
1895
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1857
				CargoID temp_cid = v->cargo_type;
3988
b76291f6ce29 (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  1858
				byte temp_subtype = v->cargo_subtype;
1895
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1859
				v->cargo_type = new_cid;
3988
b76291f6ce29 (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  1860
				v->cargo_subtype = new_subtype;
b76291f6ce29 (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  1861
				/* Check the refit capacity callback */
3390
f910bc27153e (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3355
diff changeset
  1862
				amount = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
3988
b76291f6ce29 (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  1863
				/* Restore the original cargo type */
1895
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1864
				v->cargo_type = temp_cid;
3988
b76291f6ce29 (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  1865
				v->cargo_subtype = temp_subtype;
1895
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1866
			}
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1867
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1868
			if (amount == CALLBACK_FAILED) { // callback failed or not used, use default
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1869
				CargoID old_cid = rvi->cargo_type;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1870
				/* normally, the capacity depends on the cargo type, a rail vehicle can
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1871
				 * carry twice as much mail/goods as normal cargo, and four times as
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1872
				 * many passengers
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1873
				 */
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1874
				amount = rvi->capacity;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1875
				switch (old_cid) {
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1876
					case CT_PASSENGERS: break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1877
					case CT_MAIL:
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1878
					case CT_GOODS: amount *= 2; break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1879
					default:       amount *= 4; break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1880
				}
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1881
				switch (new_cid) {
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1882
					case CT_PASSENGERS: break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1883
					case CT_MAIL:
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1884
					case CT_GOODS: amount /= 2; break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1885
					default:       amount /= 4; break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1886
				}
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1887
			};
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1888
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1889
			if (amount != 0) {
4242
e6baba95f369 (svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents: 4198
diff changeset
  1890
				if (new_cid != v->cargo_type) {
4544
b2d5ad597e4d (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4529
diff changeset
  1891
					cost += GetRefitCost(v->engine_type);
4242
e6baba95f369 (svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents: 4198
diff changeset
  1892
				}
e6baba95f369 (svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents: 4198
diff changeset
  1893
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1894
				num += amount;
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1895
				if (flags & DC_EXEC) {
3955
81e7e25e4599 (svn r5104) - When refitting a vehicle to its existing cargo type, don't lose the cargo onboard (useful when adding wagons to a train)
peter1138
parents: 3954
diff changeset
  1896
					v->cargo_count = (v->cargo_type == new_cid) ? min(amount, v->cargo_count) : 0;
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1897
					v->cargo_type = new_cid;
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1898
					v->cargo_cap = amount;
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1899
					v->cargo_subtype = new_subtype;
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1900
					InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1901
					InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1902
					RebuildVehicleLists();
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1903
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1904
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1905
		}
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1906
	} while ((v = v->next) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1907
3008
7b519db78ce2 (svn r3588) - Refit: Use only one global to return the refit capacity rather than one per vehicle type.
peter1138
parents: 3007
diff changeset
  1908
	_returned_refit_capacity = num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1909
4708
5129e3add448 (svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents: 4656
diff changeset
  1910
	/* Update the train's cached variables */
5303
5a3fa9dfe40b (svn r7459) -Fix: make sure TrainConsistChanged() is called for the first vehicle of a chain after reffiting
glx
parents: 5279
diff changeset
  1911
	if (flags & DC_EXEC) TrainConsistChanged(GetFirstVehicleInChain(GetVehicle(p1)));
4708
5129e3add448 (svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents: 4656
diff changeset
  1912
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1913
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1914
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1915
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1916
typedef struct TrainFindDepotData {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1917
	uint best_length;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  1918
	TileIndex tile;
2475
8443e1eefe1b (svn r3001) s/Player*/const Player*/
tron
parents: 2464
diff changeset
  1919
	PlayerID owner;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1920
	/**
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1921
	 * true if reversing is necessary for the train to get to this depot
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1922
	 * This value is unused when new depot finding and NPF are both disabled
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1923
	 */
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1924
	bool reverse;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1925
} TrainFindDepotData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1926
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1927
static bool NtpCallbFindDepot(TileIndex tile, TrainFindDepotData *tfdd, int track, uint length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1928
{
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  1929
	if (IsTileType(tile, MP_RAILWAY) &&
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  1930
			IsTileOwner(tile, tfdd->owner) &&
4182
20d711816338 (svn r5624) Use {IsPlainRailTile,IsRailDepot,IsRailWaypoint,HasSignals}() instead of GetRailTile{T,Subt}ype() - this is more concise and a bit more flexible if/when the rail tile encoding changes
tron
parents: 4095
diff changeset
  1931
			IsRailDepot(tile)) {
4406
4ff711f834b9 (svn r6159) -Fix: FindClosestTrainDepot hardly ever found a depot with NPF off due to absence of distance-normalization (Rojer)
Darkvater
parents: 4389
diff changeset
  1932
		/* approximate number of tiles by dividing by DIAG_FACTOR */
4ff711f834b9 (svn r6159) -Fix: FindClosestTrainDepot hardly ever found a depot with NPF off due to absence of distance-normalization (Rojer)
Darkvater
parents: 4389
diff changeset
  1933
		tfdd->best_length = length / DIAG_FACTOR;
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  1934
		tfdd->tile = tile;
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  1935
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1936
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1937
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1938
	return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1939
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1940
1758
68668114f92e (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
  1941
// returns the tile of a depot to goto to. The given vehicle must not be
68668114f92e (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
  1942
// crashed!
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1943
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v, int max_distance)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1944
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1945
	TrainFindDepotData tfdd;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  1946
	TileIndex tile = v->tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1947
1758
68668114f92e (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
  1948
	assert(!(v->vehstatus & VS_CRASHED));
68668114f92e (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
  1949
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1950
	tfdd.owner = v->owner;
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1951
	tfdd.best_length = (uint)-1;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1952
	tfdd.reverse = false;
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1953
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1954
	if (IsTileDepotType(tile, TRANSPORT_RAIL)){
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1955
		tfdd.tile = tile;
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1956
		tfdd.best_length = 0;
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1957
		return tfdd;
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1958
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1959
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  1960
	if (v->u.rail.track == 0x40) tile = GetVehicleOutOfTunnelTile(v);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  1961
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1962
	if (_patches.yapf.rail_use_yapf) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1963
		bool found = YapfFindNearestRailDepotTwoWay(v, max_distance, NPF_INFINITE_PENALTY, &tfdd.tile, &tfdd.reverse);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1964
		tfdd.best_length = found ? max_distance / 2 : -1; // some fake distance or NOT_FOUND
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1965
	} else if (_patches.new_pathfinding_all) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1966
		NPFFoundTargetData ftd;
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1967
		Vehicle* last = GetLastVehicleInChain(v);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1968
		Trackdir trackdir = GetVehicleTrackdir(v);
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1969
		Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1970
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1971
		assert (trackdir != INVALID_TRACKDIR);
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  1972
		ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, last->tile, trackdir_rev, TRANSPORT_RAIL, v->owner, v->u.rail.compatible_railtypes, NPF_INFINITE_PENALTY);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1973
		if (ftd.best_bird_dist == 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1974
			/* Found target */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1975
			tfdd.tile = ftd.node.tile;
1675
d004a4d0f6d4 (svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents: 1615
diff changeset
  1976
			/* Our caller expects a number of tiles, so we just approximate that
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1977
			 * number by this. It might not be completely what we want, but it will
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1978
			 * work for now :-) We can possibly change this when the old pathfinder
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1979
			 * is removed. */
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1980
			tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  1981
			if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) tfdd.reverse = true;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1982
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1983
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1984
		// search in the forward direction first.
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  1985
		DiagDirection i;
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  1986
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  1987
		i = DirToDiagDir(v->direction);
3163
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1988
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) {
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1989
			i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT);
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1990
		}
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  1991
		NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1992
		if (tfdd.best_length == (uint)-1){
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1993
			tfdd.reverse = true;
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1994
			// search in backwards direction
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  1995
			i = ReverseDiagDir(DirToDiagDir(v->direction));
3163
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1996
			if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) {
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1997
				i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT);
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1998
			}
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  1999
			NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2000
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2001
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2002
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2003
	return tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2004
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2005
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  2006
/** Send a train to a depot
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  2007
 * @param tile unused
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  2008
 * @param p1 train to send to the depot
4451
66603f0f732e (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
  2009
 * @param p2 various bitmasked elements
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2010
 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2011
 * - p2 bit 8-10 - VLW flag (for mass goto depot)
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  2012
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  2013
int32 CmdSendTrainToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2014
{
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  2015
	Vehicle *v;
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2016
	TrainFindDepotData tfdd;
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2017
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2018
	if (p2 & DEPOT_MASS_SEND) {
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2019
		/* Mass goto depot requested */
4546
9345e0569655 (svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents: 4544
diff changeset
  2020
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
9345e0569655 (svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents: 4544
diff changeset
  2021
		return SendAllVehiclesToDepot(VEH_Train, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
4463
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  2022
	}
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  2023
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2024
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  2025
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  2026
	v = GetVehicle(p1);
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  2027
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2028
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2029
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2030
	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
1757
b179d50241dc (svn r2261) - Fix: When crashed vehicles try to find a depot for servicing, openttd asserts.
matthijs
parents: 1752
diff changeset
  2031
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2032
	if (v->current_order.type == OT_GOTO_DEPOT) {
4519
1437b1e7416c (svn r6304) -Codechange r6295: Use !! instead of (bool) in commands to send vehicles to depots
bjarni
parents: 4510
diff changeset
  2033
		if (!!(p2 & DEPOT_SERVICE) == HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT)) {
4510
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  2034
			/* We called with a different DEPOT_SERVICE setting.
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  2035
			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  2036
			 * Note: the if is (true for requesting service == true for ordered to stop in depot)          */
4510
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  2037
			if (flags & DC_EXEC) {
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  2038
				TOGGLEBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  2039
				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  2040
			}
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  2041
			return 0;
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  2042
		}
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  2043
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2044
		if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2045
		if (flags & DC_EXEC) {
1615
cee18da24b41 (svn r2119) - Fix: [ 1172878 ] Trains "Go to depot" button: click twice skip to next order (patch by Loic GUILLOUX). I also added short explanation of OF_/OFB_ difference to order.h.
pasky
parents: 1601
diff changeset
  2046
			if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2047
				v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2048
				v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2049
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2050
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2051
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2052
			v->current_order.flags = 0;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2053
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2054
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2055
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2056
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2057
4526
77ef74d28e76 (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
  2058
	/* check if at a standstill (not stopped only) in a depot
77ef74d28e76 (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
  2059
	 * the check is down here to make it possible to alter stop/service for trains entering the depot */
77ef74d28e76 (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
  2060
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL) && v->cur_speed == 0) return CMD_ERROR;
77ef74d28e76 (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
  2061
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2062
	tfdd = FindClosestTrainDepot(v, 0);
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2063
	if (tfdd.best_length == (uint)-1) return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2064
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2065
	if (flags & DC_EXEC) {
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2066
		v->dest_tile = tfdd.tile;
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2067
		v->current_order.type = OT_GOTO_DEPOT;
4412
945e8a9bb89a (svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents: 4406
diff changeset
  2068
		v->current_order.flags = OF_NON_STOP;
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2069
		if (!(p2 & DEPOT_SERVICE)) SETBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2070
		v->current_order.dest = GetDepotByTile(tfdd.tile)->index;
5259
08336337aae0 (svn r7389) -Fix: when forcing a vehicle to go to a depot, clear refit orders from current orders (caused refit error popup when not having no orders)
bjarni
parents: 5256
diff changeset
  2071
		v->current_order.refit_cargo = CT_INVALID;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2072
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
1777
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  2073
		/* If there is no depot in front, reverse automatically */
d328484bd6f2 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  2074
		if (tfdd.reverse)
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  2075
			DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2076
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2077
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2078
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2079
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2080
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2081
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1087
diff changeset
  2082
void OnTick_Train(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2083
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2084
	_age_cargo_skip_counter = (_age_cargo_skip_counter == 0) ? 184 : (_age_cargo_skip_counter - 1);
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
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2087
static const int8 _vehicle_smoke_pos[8] = {
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2088
	1, 1, 1, 0, -1, -1, -1, 0
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
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  2091
static void HandleLocomotiveSmokeCloud(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2092
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  2093
	const Vehicle* u;
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2094
	bool sound = false;
0
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
	if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2097
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2098
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2099
	u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2100
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2101
	do {
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
  2102
		EngineID engtype = v->engine_type;
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2103
		int effect_offset = GB(v->u.rail.cached_vis_effect, 0, 4) - 8;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2104
		byte effect_type = GB(v->u.rail.cached_vis_effect, 4, 2);
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2105
		bool disable_effect = HASBIT(v->u.rail.cached_vis_effect, 6);
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2106
		int x, y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2107
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2108
		// no smoke?
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2109
		if ((RailVehInfo(engtype)->flags & RVI_WAGON && effect_type == 0) ||
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2110
				disable_effect ||
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  2111
				GetEngine(engtype)->railtype > RAILTYPE_ELECTRIC ||
3546
1cb79c89c9a9 (svn r4412) When a vehicle is in a depot or tunnel it's always flagged as VS_HIDDEN. So after checking for VS_HIDDEN and the result is false there's no need to check if it's flagged as being in a depot or tunnel
tron
parents: 3491
diff changeset
  2112
				v->vehstatus & VS_HIDDEN) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2113
			continue;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2114
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2115
2612
744aef3af72f (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  2116
		// No smoke in depots or tunnels
3590
e36eef17cf3f (svn r4479) -Fix (FS#90) electric engines (or rather their pantographs) no longer emit sparks when engine is pulled on convrail (MeusH)
celestar
parents: 3560
diff changeset
  2117
		if (IsTileDepotType(v->tile, TRANSPORT_RAIL) || IsTunnelTile(v->tile)) continue;
e36eef17cf3f (svn r4479) -Fix (FS#90) electric engines (or rather their pantographs) no longer emit sparks when engine is pulled on convrail (MeusH)
celestar
parents: 3560
diff changeset
  2118
e36eef17cf3f (svn r4479) -Fix (FS#90) electric engines (or rather their pantographs) no longer emit sparks when engine is pulled on convrail (MeusH)
celestar
parents: 3560
diff changeset
  2119
		// No sparks for electric vehicles on nonelectrified tracks
e36eef17cf3f (svn r4479) -Fix (FS#90) electric engines (or rather their pantographs) no longer emit sparks when engine is pulled on convrail (MeusH)
celestar
parents: 3560
diff changeset
  2120
		if (!HasPowerOnRail(v->u.rail.railtype, GetTileRailType(v->tile, GetVehicleTrackdir(v)))) continue;
2612
744aef3af72f (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  2121
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2122
		if (effect_type == 0) {
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2123
			// Use default effect type for engine class.
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2124
			effect_type = RailVehInfo(engtype)->engclass;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2125
		} else {
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2126
			effect_type--;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2127
		}
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2128
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2129
		x = _vehicle_smoke_pos[v->direction] * effect_offset;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2130
		y = _vehicle_smoke_pos[(v->direction + 2) % 8] * effect_offset;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2131
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2132
		if (HASBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION)) {
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2133
			x = -x;
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2134
			y = -y;
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2135
		}
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2136
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2137
		switch (effect_type) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2138
		case 0:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2139
			// steam smoke.
2612
744aef3af72f (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  2140
			if (GB(v->tick_counter, 0, 4) == 0) {
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2141
				CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE);
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2142
				sound = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2143
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2144
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2145
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2146
		case 1:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2147
			// diesel smoke
2637
8807e6a63f89 (svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents: 2635
diff changeset
  2148
			if (u->cur_speed <= 40 && CHANCE16(15, 128)) {
1359
8ba976aed634 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1337
diff changeset
  2149
				CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2150
				sound = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2151
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2152
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2153
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2154
		case 2:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2155
			// blue spark
2637
8807e6a63f89 (svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents: 2635
diff changeset
  2156
			if (GB(v->tick_counter, 0, 2) == 0 && CHANCE16(1, 45)) {
1359
8ba976aed634 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1337
diff changeset
  2157
				CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2158
				sound = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2159
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2160
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2161
		}
2637
8807e6a63f89 (svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents: 2635
diff changeset
  2162
	} while ((v = v->next) != NULL);
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2163
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2164
	if (sound) PlayVehicleSound(u, VSE_TRAIN_EFFECT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2165
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2166
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2167
static void TrainPlayLeaveStationSound(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2168
{
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2169
	static const SoundFx sfx[] = {
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2170
		SND_04_TRAIN,
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2171
		SND_0A_TRAIN_HORN,
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2172
		SND_0A_TRAIN_HORN
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2173
	};
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2174
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
  2175
	EngineID engtype = v->engine_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2176
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2177
	if (PlayVehicleSound(v, VSE_START)) return;
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2178
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1925
diff changeset
  2179
	switch (GetEngine(engtype)->railtype) {
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
  2180
		case RAILTYPE_RAIL:
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  2181
		case RAILTYPE_ELECTRIC:
540
fbed599dadbb (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
  2182
			SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], v);
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  2183
			break;
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
  2184
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2185
		case RAILTYPE_MONO: SndPlayVehicleFx(SND_47_MAGLEV_2, v); break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2186
		case RAILTYPE_MAGLEV: SndPlayVehicleFx(SND_41_MAGLEV, v); break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2187
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2188
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2189
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2190
static bool CheckTrainStayInDepot(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2191
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2192
	Vehicle *u;
495
bfc16f011bb7 (svn r785) -Fix: A train can leave and enter the same depot at the same time, then the trai simply got stuck
tron
parents: 491
diff changeset
  2193
bfc16f011bb7 (svn r785) -Fix: A train can leave and enter the same depot at the same time, then the trai simply got stuck
tron
parents: 491
diff changeset
  2194
	// bail out if not all wagons are in the same depot or not in a depot at all
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2195
	for (u = v; u != NULL; u = u->next) {
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2196
		if (u->u.rail.track != 0x80 || u->tile != v->tile) return false;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2197
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2198
4252
e06bade21a2e (svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents: 4251
diff changeset
  2199
	// if the train got no power, then keep it in the depot
e06bade21a2e (svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents: 4251
diff changeset
  2200
	if (v->u.rail.cached_power == 0) {
e06bade21a2e (svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents: 4251
diff changeset
  2201
		v->vehstatus |= VS_STOPPED;
e06bade21a2e (svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents: 4251
diff changeset
  2202
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
e06bade21a2e (svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents: 4251
diff changeset
  2203
		return true;
e06bade21a2e (svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents: 4251
diff changeset
  2204
	}
e06bade21a2e (svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents: 4251
diff changeset
  2205
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2206
	if (v->u.rail.force_proceed == 0) {
1151
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2207
		if (++v->load_unload_time_rem < 37) {
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2208
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2209
			return true;
1151
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2210
		}
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2211
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2212
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2213
3172
1605cb59ccd9 (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3163
diff changeset
  2214
		if (UpdateSignalsOnSegment(v->tile, DirToDiagDir(v->direction))) {
1151
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2215
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2216
			return true;
1151
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2217
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2218
	}
2916
8f1aa489701f (svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents: 2883
diff changeset
  2219
578
86e352980acd (svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents: 555
diff changeset
  2220
	VehicleServiceInDepot(v);
1151
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2221
	InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2222
	TrainPlayLeaveStationSound(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2223
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2224
	v->u.rail.track = 1;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2225
	if (v->direction & 2) v->u.rail.track = 2;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2226
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2227
	v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2228
	v->cur_speed = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2229
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2230
	UpdateTrainDeltaXY(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2231
	v->cur_image = GetTrainImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2232
	VehiclePositionChanged(v);
3172
1605cb59ccd9 (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3163
diff changeset
  2233
	UpdateSignalsOnSegment(v->tile, DirToDiagDir(v->direction));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2234
	UpdateTrainAcceleration(v);
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  2235
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2236
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2237
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2238
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2239
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2240
/* Check for station tiles */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2241
typedef struct TrainTrackFollowerData {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2242
	TileIndex dest_coords;
1551
eb05e6a90e67 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  2243
	StationID station_index; // station index we're heading for
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2244
	uint best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2245
	uint best_track_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2246
	byte best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2247
} TrainTrackFollowerData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2248
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2249
static bool NtpCallbFindStation(TileIndex tile, TrainTrackFollowerData *ttfd, int track, uint length)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  2250
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2251
	// heading for nowhere?
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2928
diff changeset
  2252
	if (ttfd->dest_coords == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2253
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2254
	// did we reach the final station?
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  2255
	if ((ttfd->station_index == INVALID_STATION && tile == ttfd->dest_coords) || (
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  2256
				IsTileType(tile, MP_STATION) &&
3334
b72ac8637a30 (svn r4113) Moved all relevant map functions for stations to station_map.[ch]
celestar
parents: 3322
diff changeset
  2257
				IsRailwayStation(tile) &&
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  2258
				GetStationIndex(tile) == ttfd->station_index
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  2259
			)) {
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2260
		/* We do not check for dest_coords if we have a station_index,
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2261
		 * because in that case the dest_coords are just an
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2262
		 * approximation of where the station is */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2263
		// found station
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2264
		ttfd->best_track = track;
5467
24a2a4d58223 (svn r9350) [0.5] -Backport from trunk (r9105, r9115, r9117, r9146):
rubidium
parents: 5463
diff changeset
  2265
		ttfd->best_bird_dist = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2266
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2267
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2268
		uint dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2269
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2270
		// didn't find station, keep track of the best path so far.
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1237
diff changeset
  2271
		dist = DistanceManhattan(tile, ttfd->dest_coords);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2272
		if (dist < ttfd->best_bird_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2273
			ttfd->best_bird_dist = dist;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2274
			ttfd->best_track = track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2275
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2276
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2277
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2278
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2279
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  2280
static void FillWithStationData(TrainTrackFollowerData* fd, const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2281
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2282
	fd->dest_coords = v->dest_tile;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2283
	if (v->current_order.type == OT_GOTO_STATION) {
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2284
		fd->station_index = v->current_order.dest;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2285
	} else {
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2286
		fd->station_index = INVALID_STATION;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2287
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2288
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2289
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2290
static const byte _initial_tile_subcoord[6][4][3] = {
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2291
{{ 15, 8, 1 }, { 0, 0, 0 }, { 0, 8, 5 }, { 0,  0, 0 }},
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2292
{{  0, 0, 0 }, { 8, 0, 3 }, { 0, 0, 0 }, { 8, 15, 7 }},
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2293
{{  0, 0, 0 }, { 7, 0, 2 }, { 0, 7, 6 }, { 0,  0, 0 }},
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2294
{{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }},
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2295
{{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0,  0, 0 }},
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2296
{{  0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2297
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2298
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2299
static const uint32 _reachable_tracks[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2300
	0x10091009,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2301
	0x00160016,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2302
	0x05200520,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2303
	0x2A002A00,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2304
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2305
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2306
static const byte _search_directions[6][4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2307
	{ 0, 9, 2, 9 }, // track 1
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2308
	{ 9, 1, 9, 3 }, // track 2
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2309
	{ 9, 0, 3, 9 }, // track upper
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2310
	{ 1, 9, 9, 2 }, // track lower
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2311
	{ 3, 2, 9, 9 }, // track left
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2312
	{ 9, 9, 1, 0 }, // track right
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2313
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2314
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2315
static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2316
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2317
/* choose a track */
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  2318
static byte ChooseTrainTrack(Vehicle* v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirbits)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2319
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2320
	TrainTrackFollowerData fd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2321
	uint best_track;
4870
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2322
	// pathfinders are able to tell that route was only 'guessed'
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2323
	bool path_not_found = false;
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2324
2758
9cd8a21e9a7b (svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents: 2752
diff changeset
  2325
#ifdef PF_BENCHMARK
3341
b8febc9509d7 (svn r4125) - Feature: Add a general TIC() TOC() mechanism using rdtsc or something similar on non-i386 architectures to performance-tune (critical) code. Some systems are probably missing, but those can be added later.
Darkvater
parents: 3334
diff changeset
  2326
	TIC()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2327
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2328
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2329
	assert((trackdirbits & ~0x3F) == 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2330
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2331
	/* quick return in case only one possible track is available */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2332
	if (KILL_FIRST_BIT(trackdirbits) == 0) return FIND_FIRST_BIT(trackdirbits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2333
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2334
	if (_patches.yapf.rail_use_yapf) {
4870
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2335
		Trackdir trackdir = YapfChooseRailTrack(v, tile, enterdir, trackdirbits, &path_not_found);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2336
		if (trackdir != INVALID_TRACKDIR) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2337
			best_track = TrackdirToTrack(trackdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2338
		} else {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2339
			best_track = FIND_FIRST_BIT(TrackdirBitsToTrackBits(trackdirbits));
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2340
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2341
	} else if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2342
		void* perf = NpfBeginInterval();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2343
		int time = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2344
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2345
		NPFFindStationOrTileData fstd;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2346
		NPFFoundTargetData ftd;
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2347
		Trackdir trackdir;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2348
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2349
		NPFFillWithOrderData(&fstd, v);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2350
		/* The enterdir for the new tile, is the exitdir for the old tile */
1752
cdbfb2f23e72 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1698
diff changeset
  2351
		trackdir = GetVehicleTrackdir(v);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2352
		assert(trackdir != 0xff);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2353
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4556
diff changeset
  2354
		ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.compatible_railtypes);
1698
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2355
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2356
		if (ftd.best_trackdir == 0xff) {
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2357
			/* We are already at our target. Just do something */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2358
			//TODO: maybe display error?
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2359
			//TODO: go straight ahead if possible?
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2360
			best_track = FIND_FIRST_BIT(trackdirbits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2361
		} else {
1698
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2362
			/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2363
			the direction we need to take to get there, if ftd.best_bird_dist is not 0,
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2364
			we did not find our target, but ftd.best_trackdir contains the direction leading
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2365
			to the tile closest to our target. */
4870
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2366
			if (ftd.best_bird_dist != 0) path_not_found = true;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2367
			/* Discard enterdir information, making it a normal track */
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2368
			best_track = TrackdirToTrack(ftd.best_trackdir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2369
		}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2370
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2371
		time = NpfEndInterval(perf);
3947
7ab994b0fe9c (svn r5093) -CodeChange: [YAPF] min. debug level changed from 1 to 3 and 4 for frequent debug messages (performance stats)
KUDr
parents: 3934
diff changeset
  2372
		DEBUG(yapf, 4)("[YAPF][NPFT] %d us - %d rounds - %d open - %d closed -- ", time, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2373
	} else {
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2374
		void* perf = NpfBeginInterval();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2375
		int time = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2376
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2377
		FillWithStationData(&fd, v);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2378
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2379
		/* New train pathfinding */
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2380
		fd.best_bird_dist = (uint)-1;
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2381
		fd.best_track_dist = (uint)-1;
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2382
		fd.best_track = 0xFF;
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2383
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4556
diff changeset
  2384
		NewTrainPathfind(tile - TileOffsByDiagDir(enterdir), v->dest_tile,
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  2385
			v->u.rail.compatible_railtypes, enterdir, (NTPEnumProc*)NtpCallbFindStation, &fd);
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2386
4870
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2387
		// check whether the path was found or only 'guessed'
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2388
		if (fd.best_bird_dist != 0) path_not_found = true;
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2389
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2390
		if (fd.best_track == 0xff) {
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2391
			// blaha
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2392
			best_track = FIND_FIRST_BIT(trackdirbits);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2393
		} else {
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2394
			best_track = fd.best_track & 7;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2395
		}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2396
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2397
		time = NpfEndInterval(perf);
3947
7ab994b0fe9c (svn r5093) -CodeChange: [YAPF] min. debug level changed from 1 to 3 and 4 for frequent debug messages (performance stats)
KUDr
parents: 3934
diff changeset
  2398
		DEBUG(yapf, 4)("[YAPF][NTPT] %d us - %d rounds - %d open - %d closed -- ", time, 0, 0, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2399
	}
4870
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2400
	// handle "path not found" state
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2401
	if (path_not_found) {
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2402
		// PF didn't find the route
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2403
		if (!HASBIT(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) {
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2404
			// it is first time the problem occurred, set the "path not found" flag
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2405
			SETBIT(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION);
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2406
			// and notify user about the event
4996
c20caeeb1aed (svn r7003) -Fix: only the appropriate train owner can now see the "Train is lost" message (peter1138)
KUDr
parents: 4870
diff changeset
  2407
			if (_patches.lost_train_warn && v->owner == _local_player) {
4870
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2408
				SetDParam(0, v->unitnumber);
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2409
				AddNewsItem(
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2410
					STR_TRAIN_IS_LOST,
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2411
					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2412
					v->index,
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2413
					0);
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2414
			}
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2415
		}
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2416
	} else {
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2417
		// route found, is the train marked with "path not found" flag?
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2418
		if (HASBIT(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) {
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2419
			// clear the flag as the PF's problem was solved
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2420
			CLRBIT(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION);
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2421
			// can we also delete the "News" item somehow?
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2422
		}
0ee22ed51ada (svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents: 4856
diff changeset
  2423
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2424
2758
9cd8a21e9a7b (svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents: 2752
diff changeset
  2425
#ifdef PF_BENCHMARK
3341
b8febc9509d7 (svn r4125) - Feature: Add a general TIC() TOC() mechanism using rdtsc or something similar on non-i386 architectures to performance-tune (critical) code. Some systems are probably missing, but those can be added later.
Darkvater
parents: 3334
diff changeset
  2426
	TOC("PF time = ", 1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2427
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2428
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2429
	return best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2430
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2431
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2432
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2433
static bool CheckReverseTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2434
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2435
	TrainTrackFollowerData fd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2436
	int i, r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2437
	int best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2438
	uint best_bird_dist  = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2439
	uint best_track_dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2440
	uint reverse, reverse_best;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2441
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2442
	if (_opt.diff.line_reverse_mode != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2443
			v->u.rail.track & 0xC0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2444
			!(v->direction & 1))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2445
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2446
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2447
	FillWithStationData(&fd, v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2448
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2449
	best_track = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2450
	reverse_best = reverse = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2451
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2452
	assert(v->u.rail.track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2453
3161
0274072d39b4 (svn r3787) Use DirToDiagDir() instead of >> 1
tron
parents: 3160
diff changeset
  2454
	i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][DirToDiagDir(v->direction)];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2455
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2456
	if (_patches.yapf.rail_use_yapf) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2457
		reverse_best = YapfCheckReverseTrain(v);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2458
	} else if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2459
		NPFFindStationOrTileData fstd;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2460
		NPFFoundTargetData ftd;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2461
		byte trackdir, trackdir_rev;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2462
		Vehicle* last = GetLastVehicleInChain(v);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2463
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2464
		NPFFillWithOrderData(&fstd, v);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2465
1752
cdbfb2f23e72 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1698
diff changeset
  2466
		trackdir = GetVehicleTrackdir(v);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2467
		trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2468
		assert(trackdir != 0xff);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2469
		assert(trackdir_rev != 0xff);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2470
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  2471
		ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, last->tile, trackdir_rev, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.compatible_railtypes);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2472
		if (ftd.best_bird_dist != 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2473
			/* We didn't find anything, just keep on going straight ahead */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2474
			reverse_best = false;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2475
		} else {
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2476
			if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2477
				reverse_best = true;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2478
			} else {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2479
				reverse_best = false;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2480
			}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2481
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2482
	} else {
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  2483
		for (;;) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2484
			fd.best_bird_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2485
			fd.best_track_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2486
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  2487
			NewTrainPathfind(v->tile, v->dest_tile, v->u.rail.compatible_railtypes, reverse ^ i, (NTPEnumProc*)NtpCallbFindStation, &fd);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2488
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2489
			if (best_track != -1) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2490
				if (best_bird_dist != 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2491
					if (fd.best_bird_dist != 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2492
						/* neither reached the destination, pick the one with the smallest bird dist */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2493
						if (fd.best_bird_dist > best_bird_dist) goto bad;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2494
						if (fd.best_bird_dist < best_bird_dist) goto good;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2495
					} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2496
						/* we found the destination for the first time */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2497
						goto good;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2498
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2499
				} else {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2500
					if (fd.best_bird_dist != 0) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2501
						/* didn't find destination, but we've found the destination previously */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2502
						goto bad;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2503
					} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2504
						/* both old & new reached the destination, compare track length */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2505
						if (fd.best_track_dist > best_track_dist) goto bad;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2506
						if (fd.best_track_dist < best_track_dist) goto good;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2507
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2508
				}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2509
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2510
				/* if we reach this position, there's two paths of equal value so far.
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2511
				 * pick one randomly. */
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  2512
				r = GB(Random(), 0, 8);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2513
				if (_pick_track_table[i] == (v->direction & 3)) r += 80;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2514
				if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2515
				if (r <= 127) goto bad;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2516
			}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2517
good:;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2518
			best_track = i;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2519
			best_bird_dist = fd.best_bird_dist;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2520
			best_track_dist = fd.best_track_dist;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2521
			reverse_best = reverse;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2522
bad:;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2523
			if (reverse != 0) break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2524
			reverse = 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2525
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2526
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2527
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2528
	return reverse_best != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2529
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2530
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2531
static bool ProcessTrainOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2532
{
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2533
	const Order *order;
3934
6d178534fbaf (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2534
	bool at_waypoint = false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2535
3005
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2536
	switch (v->current_order.type) {
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2537
		case OT_GOTO_DEPOT:
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2538
			if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return false;
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2539
			if ((v->current_order.flags & OF_SERVICE_IF_NEEDED) &&
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2540
					!VehicleNeedsService(v)) {
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2541
				v->cur_order_index++;
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2542
			}
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2543
			break;
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2544
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2545
		case OT_LOADING:
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2546
		case OT_LEAVESTATION:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2547
			return false;
4351
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
  2548
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
  2549
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2550
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2551
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: 358
diff changeset
  2552
	// check if we've reached the waypoint?
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2553
	if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2554
		v->cur_order_index++;
3934
6d178534fbaf (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2555
		at_waypoint = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2556
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2557
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2558
	// check if we've reached a non-stop station while TTDPatch nonstop is enabled..
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2559
	if (_patches.new_nonstop &&
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2560
			v->current_order.flags & OF_NON_STOP &&
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2561
			IsTileType(v->tile, MP_STATION) &&
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2562
			v->current_order.dest == GetStationIndex(v->tile)) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2563
		v->cur_order_index++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2564
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2565
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2566
	// Get the current order
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2567
	if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2568
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2569
	order = GetVehicleOrder(v, v->cur_order_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2570
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2571
	// If no order, do nothing.
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2572
	if (order == NULL) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2573
		v->current_order.type = OT_NOTHING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2574
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2575
		v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2576
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2577
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2578
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2579
	// If it is unchanged, keep it.
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2580
	if (order->type  == v->current_order.type &&
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2581
			order->flags == v->current_order.flags &&
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2582
			order->dest  == v->current_order.dest)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2583
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2584
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2585
	// Otherwise set it, and determine the destination tile.
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2586
	v->current_order = *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2587
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2588
	v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2589
3934
6d178534fbaf (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2590
	InvalidateVehicleOrder(v);
6d178534fbaf (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2591
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2592
	switch (order->type) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2593
		case OT_GOTO_STATION:
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2594
			if (order->dest == v->last_station_visited)
1266
9dc5638fe8cc (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
  2595
				v->last_station_visited = INVALID_STATION;
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2596
			v->dest_tile = GetStation(order->dest)->xy;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2597
			break;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2598
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2599
		case OT_GOTO_DEPOT:
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2600
			v->dest_tile = GetDepot(order->dest)->xy;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2601
			break;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2602
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2603
		case OT_GOTO_WAYPOINT:
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2604
			v->dest_tile = GetWaypoint(order->dest)->xy;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2605
			break;
3934
6d178534fbaf (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2606
6d178534fbaf (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2607
		default:
6d178534fbaf (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2608
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2609
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2610
3934
6d178534fbaf (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2611
	return !at_waypoint && CheckReverseTrain(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2612
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2613
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2614
static void MarkTrainDirty(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2615
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2616
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2617
		v->cur_image = GetTrainImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2618
		MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2619
	} while ((v = v->next) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2620
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2621
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2622
static void HandleTrainLoading(Vehicle *v, bool mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2623
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2624
	if (v->current_order.type == OT_NOTHING) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2625
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2626
	if (v->current_order.type != OT_DUMMY) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2627
		if (v->current_order.type != OT_LOADING) return;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2628
		if (mode) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2629
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2630
		// don't mark the train as lost if we're loading on the final station.
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2631
		if (v->current_order.flags & OF_NON_STOP)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2632
			v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2633
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2634
		if (--v->load_unload_time_rem) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2635
5211
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5198
diff changeset
  2636
		if (CanFillVehicle(v) && (v->current_order.flags & OF_FULL_LOAD ||
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5198
diff changeset
  2637
				(_patches.gradual_loading && !HASBIT(v->load_status, LS_LOADING_FINISHED)))) {
882
b6c4b9afa21b (svn r1367) -Fix: Full-Loading trains no longer get "lost" after a while (Hackykid)
matthijs
parents: 842
diff changeset
  2638
			v->u.rail.days_since_order_progr = 0; /* Prevent a train lost message for full loading trains */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2639
			SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC);
5211
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5198
diff changeset
  2640
			if (LoadUnloadVehicle(v, false)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2641
				InvalidateWindow(WC_TRAINS_LIST, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2642
				MarkTrainDirty(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2643
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  2644
				// need to update acceleration and cached values since the goods on the train changed.
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  2645
				TrainCargoChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2646
				UpdateTrainAcceleration(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2647
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2648
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2649
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2650
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2651
		TrainPlayLeaveStationSound(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2652
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2653
		{
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2654
			Order b = v->current_order;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2655
			v->current_order.type = OT_LEAVESTATION;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2656
			v->current_order.flags = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2657
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2658
			// If this was not the final order, don't remove it from the list.
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2659
			if (!(b.flags & OF_NON_STOP)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2660
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2661
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2662
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2663
	v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2664
	v->cur_order_index++;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2665
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2666
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2667
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2668
static int UpdateTrainSpeed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2669
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2670
	uint spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2671
	uint accel;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2672
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2673
	if (v->vehstatus & VS_STOPPED || HASBIT(v->u.rail.flags, VRF_REVERSING)) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2674
		if (_patches.realistic_acceleration) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2675
			accel = GetTrainAcceleration(v, AM_BRAKE) * 2;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2676
		} else {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2677
			accel = v->acceleration * -2;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2678
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2679
	} else {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2680
		if (_patches.realistic_acceleration) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2681
			accel = GetTrainAcceleration(v, AM_ACCEL);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2682
		} else {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2683
			accel = v->acceleration;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2684
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2685
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2686
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2687
	spd = v->subspeed + accel * 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2688
	v->subspeed = (byte)spd;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2689
	{
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2690
		int tempmax = v->max_speed;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2691
		if (v->cur_speed > v->max_speed)
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2692
			tempmax = v->cur_speed - (v->cur_speed / 10) - 1;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2693
		v->cur_speed = spd = clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax);
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2694
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2695
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2696
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2697
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2698
	spd += v->progress;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2699
	v->progress = (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2700
	return (spd >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2701
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2702
1551
eb05e6a90e67 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  2703
static void TrainEnterStation(Vehicle *v, StationID station)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2704
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2705
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2706
	uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2707
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2708
	v->last_station_visited = station;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2709
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2710
	/* check if a train ever visited this station before */
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  2711
	st = GetStation(station);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2712
	if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2713
		st->had_vehicle_of_type |= HVOT_TRAIN;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  2714
		SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2715
		flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2716
		AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2717
			STR_8801_CITIZENS_CELEBRATE_FIRST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2718
			flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2719
			v->index,
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2720
			0
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2721
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2722
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2723
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2724
	// Did we reach the final destination?
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2725
	if (v->current_order.type == OT_GOTO_STATION &&
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2726
			v->current_order.dest == station) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2727
		// Yeah, keep the load/unload flags
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2728
		// Non Stop now means if the order should be increased.
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2729
		v->current_order.type = OT_LOADING;
1935
f43f062c9498 (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1934
diff changeset
  2730
		v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD | OF_TRANSFER;
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2731
		v->current_order.flags |= OF_NON_STOP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2732
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2733
		// No, just do a simple load
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2734
		v->current_order.type = OT_LOADING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2735
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2736
	}
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2737
	v->current_order.dest = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2738
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2739
	SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC);
5211
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5198
diff changeset
  2740
	if (LoadUnloadVehicle(v, true) != 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2741
		InvalidateWindow(WC_TRAINS_LIST, v->owner);
1905
f43d9e821deb (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  2742
		TrainCargoChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2743
		UpdateTrainAcceleration(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2744
	}
2830
873da5589c15 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2826
diff changeset
  2745
	MarkTrainDirty(v);
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2746
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2747
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2748
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2749
static byte AfterSetTrainPos(Vehicle *v, bool new_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2750
{
5519
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2751
	byte old_z = v->z_pos;
5522
faeba89423b5 (svn r10383) [0.5] -Fix (r10373, FS#958): v->z_pos was wrong for trains on bridge
glx
parents: 5519
diff changeset
  2752
faeba89423b5 (svn r10383) [0.5] -Fix (r10373, FS#958): v->z_pos was wrong for trains on bridge
glx
parents: 5519
diff changeset
  2753
	/* need this hint so it returns the right z coordinate on bridges. */
faeba89423b5 (svn r10383) [0.5] -Fix (r10373, FS#958): v->z_pos was wrong for trains on bridge
glx
parents: 5519
diff changeset
  2754
	_get_z_hint = old_z;
5519
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2755
	v->z_pos = GetSlopeZ(v->x_pos, v->y_pos);
5522
faeba89423b5 (svn r10383) [0.5] -Fix (r10373, FS#958): v->z_pos was wrong for trains on bridge
glx
parents: 5519
diff changeset
  2756
	_get_z_hint = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2757
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2758
	if (new_tile) {
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2759
		CLRBIT(v->u.rail.flags, VRF_GOINGUP);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2760
		CLRBIT(v->u.rail.flags, VRF_GOINGDOWN);
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2761
5519
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2762
		if (v->u.rail.track == TRACK_BIT_X || v->u.rail.track == TRACK_BIT_Y) {
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2763
			/* Any track that isn't TRACK_BIT_X or TRACK_BIT_Y cannot be sloped.
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2764
			 * To check whether the current tile is sloped, and in which
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2765
			 * direction it is sloped, we get the 'z' at the center of
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2766
			 * the tile (middle_z) and the edge of the tile (old_z),
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2767
			 * which we then can compare. */
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2768
			static const int HALF_TILE_SIZE = TILE_SIZE / 2;
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2769
			static const int INV_TILE_SIZE_MASK = ~(TILE_SIZE - 1);
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2770
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2771
			byte middle_z = GetSlopeZ((v->x_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE, (v->y_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE);
5528
b14613d2096f (svn r10739) [0.5] -Fix [FS#1096]: trains going over bridges would get the "going down hill" accelerate bonus, which causes trains to go faster on bridges than they would be going on level land.
rubidium
parents: 5522
diff changeset
  2772
			TileIndex tile = TileVirtXY(v->x_pos, v->y_pos);
5519
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2773
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2774
			/* For some reason tunnel tiles are always given as sloped :(
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2775
			 * But they are not sloped... */
5528
b14613d2096f (svn r10739) [0.5] -Fix [FS#1096]: trains going over bridges would get the "going down hill" accelerate bonus, which causes trains to go faster on bridges than they would be going on level land.
rubidium
parents: 5522
diff changeset
  2776
			if (middle_z != v->z_pos && !IsTunnelTile(tile) && !(IsBridgeTile(tile) && IsBridgeMiddle(tile))) {
5519
f666f6b62978 (svn r10373) [0.5] -Backport from trunk (r10306, r10311, r10317, r10339, r10344):
rubidium
parents: 5518
diff changeset
  2777
				SETBIT(v->u.rail.flags, (middle_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN);
3184
7405329343ce (svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents: 3183
diff changeset
  2778
			}
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2779
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2780
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2781
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2782
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2783
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2784
	return old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2785
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2786
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  2787
static const Direction _new_vehicle_direction_table[11] = {
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  2788
	DIR_N , DIR_NW, DIR_W , 0,
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  2789
	DIR_NE, DIR_N , DIR_SW, 0,
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  2790
	DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2791
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2792
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  2793
static Direction GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2794
{
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
  2795
	uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
  2796
							TileX(new_tile) - TileX(old_tile) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2797
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2798
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2799
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2800
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  2801
static Direction GetNewVehicleDirection(const Vehicle *v, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2802
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2803
	uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2804
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2805
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2806
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2807
2436
177cb6a8339f (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2425
diff changeset
  2808
static int GetDirectionToVehicle(const Vehicle *v, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2809
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2810
	byte offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2811
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2812
	x -= v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2813
	if (x >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2814
		offs = (x > 2) ? 0 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2815
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2816
		offs = (x < -2) ? 2 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2817
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2818
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2819
	y -= v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2820
	if (y >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2821
		offs += ((y > 2) ? 0 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2822
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2823
		offs += ((y < -2) ? 2 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2824
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2825
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2826
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2827
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2828
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2829
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2830
/* Check if the vehicle is compatible with the specified tile */
1048
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2831
static bool CheckCompatibleRail(const Vehicle *v, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2832
{
1214
33e07bbb7779 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1209
diff changeset
  2833
	switch (GetTileType(tile)) {
1048
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2834
		case MP_RAILWAY:
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2835
		case MP_STATION:
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2836
			// normal tracks, jump to owner check
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2837
			break;
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2838
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  2839
		case MP_TUNNELBRIDGE:
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  2840
			if (IsBridge(tile) && IsBridgeMiddle(tile)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  2841
				// is train going over the bridge?
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  2842
				if (v->z_pos > GetTileMaxZ(tile)) return true;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  2843
			}
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  2844
			break;
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  2845
1048
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2846
		case MP_STREET:
2360
09e42e4ee139 (svn r2886) Rename the "owner" attribute to "m1", because when it stores an owner it is accessed by [GS]etOwner anyway and when it doesn't store an owner, but arbitrary data, accessing a field called "owner" is confusing.
tron
parents: 2261
diff changeset
  2847
			// tracks over roads, do owner check of tracks
1048
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2848
			return
2510
8a959a130698 (svn r3036) Use IsCompatibleRail() to check the rail type of level crossings (not a fix, because the old == method works so far)
tron
parents: 2493
diff changeset
  2849
				IsTileOwner(tile, v->owner) && (
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  2850
					!IsFrontEngine(v) ||
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3234
diff changeset
  2851
					IsCompatibleRail(v->u.rail.railtype, GetRailTypeCrossing(tile))
2510
8a959a130698 (svn r3036) Use IsCompatibleRail() to check the rail type of level crossings (not a fix, because the old == method works so far)
tron
parents: 2493
diff changeset
  2852
				);
1048
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2853
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2854
		default:
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2855
			return true;
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2856
	}
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2857
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2858
	return
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2859
		IsTileOwner(tile, v->owner) && (
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  2860
			!IsFrontEngine(v) ||
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  2861
			HASBIT(v->u.rail.compatible_railtypes, GetRailType(tile))
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2862
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2863
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2864
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2865
typedef struct {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2866
	byte small_turn, large_turn;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2867
	byte z_up; // fraction to remove when moving up
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2868
	byte z_down; // fraction to remove when moving down
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2869
} RailtypeSlowdownParams;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2870
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  2871
static const RailtypeSlowdownParams _railtype_slowdown[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2872
	// normal accel
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2873
	{256 / 4, 256 / 2, 256 / 4, 2}, // normal
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2874
	{256 / 4, 256 / 2, 256 / 4, 2}, // electrified
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2875
	{256 / 4, 256 / 2, 256 / 4, 2}, // monorail
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4329
diff changeset
  2876
	{0,       256 / 2, 256 / 4, 2}, // maglev
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2877
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2878
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2879
/* Modify the speed of the vehicle due to a turn */
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  2880
static void AffectSpeedByDirChange(Vehicle* v, Direction new_dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2881
{
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2882
	DirDiff diff;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2883
	const RailtypeSlowdownParams *rsp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2884
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2885
	if (_patches.realistic_acceleration) return;
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2886
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2887
	diff = DirDifference(v->direction, new_dir);
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2888
	if (diff == DIRDIFF_SAME) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2889
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2890
	rsp = &_railtype_slowdown[v->u.rail.railtype];
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2891
	v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2892
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2893
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2894
/* Modify the speed of the vehicle due to a change in altitude */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2895
static void AffectSpeedByZChange(Vehicle *v, byte old_z)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2896
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2897
	const RailtypeSlowdownParams *rsp;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2898
	if (old_z == v->z_pos || _patches.realistic_acceleration) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2899
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2900
	rsp = &_railtype_slowdown[v->u.rail.railtype];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2901
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2902
	if (old_z < v->z_pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2903
		v->cur_speed -= (v->cur_speed * rsp->z_up >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2904
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2905
		uint16 spd = v->cur_speed + rsp->z_down;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2906
		if (spd <= v->max_speed) v->cur_speed = spd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2907
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2908
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2909
3172
1605cb59ccd9 (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3163
diff changeset
  2910
static const DiagDirection _otherside_signal_directions[] = {
1605cb59ccd9 (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3163
diff changeset
  2911
	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, 0, 0,
1605cb59ccd9 (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3163
diff changeset
  2912
	DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2913
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2914
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  2915
static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2916
{
3267
591027d10884 (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3259
diff changeset
  2917
	if (IsTileType(tile, MP_RAILWAY) &&
3792
2eb8388731bf (svn r4788) - Codechange: RAILTYPE_{NORMAL,ELECTRIC,...} and RAIL_TYPE_{NORMAL,SIGNAL,...} have nearly the same name, rename RAIL_TYPE_* to RAIL_TILE_* of extra clarity
rubidium
parents: 3773
diff changeset
  2918
			GetRailTileType(tile) == RAIL_TILE_SIGNALS) {
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2919
		uint i = FindFirstBit2x64(GetTrackBits(tile) * 0x101 & _reachable_tracks[dir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2920
		UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2921
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2922
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2923
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2924
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2925
static void SetVehicleCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2926
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2927
	Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2928
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  2929
	if (v->u.rail.crash_anim_pos != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2930
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2931
	v->u.rail.crash_anim_pos++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2932
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2933
	u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2934
	BEGIN_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2935
		v->vehstatus |= VS_CRASHED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2936
	END_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2937
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2938
	InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2939
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2940
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2941
static uint CountPassengersInTrain(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2942
{
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2943
	uint num = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2944
	BEGIN_ENUM_WAGONS(v)
1067
a083a6dd1419 (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2945
		if (v->cargo_type == CT_PASSENGERS) num += v->cargo_count;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2946
	END_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2947
	return num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2948
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2949
5513
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2950
typedef struct TrainCollideChecker {
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2951
	Vehicle *v;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2952
	const Vehicle *v_skip;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2953
	uint num;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2954
} TrainCollideChecker;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2955
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2956
static void *FindTrainCollideEnum(Vehicle *v, void *data)
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2957
{
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2958
	TrainCollideChecker* tcc = (TrainCollideChecker*)data;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2959
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2960
	if (v != tcc->v &&
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2961
			v != tcc->v_skip &&
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2962
			v->type == VEH_Train &&
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2963
			v->u.rail.track != 0x80 &&
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2964
			myabs(v->z_pos - tcc->v->z_pos) < 6 &&
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2965
			myabs(v->x_pos - tcc->v->x_pos) < 6 &&
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2966
			myabs(v->y_pos - tcc->v->y_pos) < 6 ) {
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2967
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2968
		Vehicle *coll = GetFirstVehicleInChain(v);
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2969
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2970
		/* it can't collide with its own wagons */
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2971
		if (tcc->v == coll ||
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2972
			(tcc->v->u.rail.track == 0x40 && (tcc->v->direction & 2) != (v->direction & 2)))
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2973
			return NULL;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2974
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2975
		/* two drivers + passengers killed in train tcc->v (if it was not crashed already) */
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2976
		if (!(tcc->v->vehstatus & VS_CRASHED)) {
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2977
			tcc->num += 2 + CountPassengersInTrain(tcc->v);
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2978
			SetVehicleCrashed(tcc->v);
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2979
		}
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2980
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2981
		if (!(coll->vehstatus & VS_CRASHED)) {
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2982
			/* two drivers + passengers killed in train coll (if it was not crashed already) */
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2983
			tcc->num += 2 + CountPassengersInTrain(coll);
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2984
			SetVehicleCrashed(coll);
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2985
		}
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2986
	}
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2987
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2988
	return NULL;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2989
}
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  2990
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2991
/*
1434
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2992
 * Checks whether the specified train has a collision with another vehicle. If
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  2993
 * so, destroys this vehicle, and the other vehicle if its subtype has TS_Front.
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2994
 * Reports the incident in a flashy news item, modifies station ratings and
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2995
 * plays a sound.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2996
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2997
static void CheckTrainCollision(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2998
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2999
	TrainCollideChecker tcc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3000
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3001
	/* can't collide in depot */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3002
	if (v->u.rail.track == 0x80) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3003
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
  3004
	assert(v->u.rail.track == 0x40 || TileVirtXY(v->x_pos, v->y_pos) == v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3005
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3006
	tcc.v = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3007
	tcc.v_skip = v->next;
5513
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  3008
	tcc.num = 0;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  3009
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  3010
	/* find colliding vehicles */
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  3011
	VehicleFromPos(v->tile, &tcc, FindTrainCollideEnum);
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  3012
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  3013
	/* any dead -> no crash */
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  3014
	if (tcc.num == 0) return;
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  3015
0d3227e250eb (svn r10280) [0.5] -Backport from trunk (r10109, r10219, r10222, r10230, r10246, r10258):
rubidium
parents: 5509
diff changeset
  3016
	SetDParam(0, tcc.num);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3017
	AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL,
1434
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3018
		NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ACCIDENT, 0),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3019
		v->index,
1434
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3020
		0
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3021
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3022
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3023
	ModifyStationRatingAround(v->tile, v->owner, -160, 30);
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  3024
	SndPlayVehicleFx(SND_13_BIG_CRASH, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3025
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3026
1432
9086ccebb3f1 (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3027
typedef struct VehicleAtSignalData {
9086ccebb3f1 (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3028
	TileIndex tile;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  3029
	Direction direction;
1432
9086ccebb3f1 (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3030
} VehicleAtSignalData;
9086ccebb3f1 (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3031
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3032
static void *CheckVehicleAtSignal(Vehicle *v, void *data)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3033
{
1432
9086ccebb3f1 (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3034
	const VehicleAtSignalData* vasd = data;
9086ccebb3f1 (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3035
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  3036
	if (v->type == VEH_Train && IsFrontEngine(v) && v->tile == vasd->tile) {
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  3037
		DirDiff diff = ChangeDirDiff(DirDifference(v->direction, vasd->direction), DIRDIFF_90RIGHT);
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  3038
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  3039
		if (diff == DIRDIFF_90RIGHT || (v->cur_speed <= 5 && diff <= DIRDIFF_REVERSE)) return v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3040
	}
1432
9086ccebb3f1 (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3041
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3042
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3043
5252
6823da581c30 (svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents: 5215
diff changeset
  3044
static void TrainController(Vehicle *v, bool update_image)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3045
{
1961
10ce7350f76b (svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents: 1942
diff changeset
  3046
	Vehicle *prev;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3047
	GetNewVehiclePosResult gp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3048
	uint32 r, tracks,ts;
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  3049
	int i;
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  3050
	DiagDirection enterdir;
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  3051
	Direction dir;
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  3052
	Direction newdir;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  3053
	Direction chosen_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3054
	byte chosen_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3055
	byte old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3056
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3057
	/* For every vehicle after and including the given vehicle */
1961
10ce7350f76b (svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents: 1942
diff changeset
  3058
	for (prev = GetPrevVehicleInChain(v); v != NULL; prev = v, v = v->next) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3059
		BeginVehicleMove(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3060
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3061
		if (v->u.rail.track != 0x40) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3062
			/* Not inside tunnel */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3063
			if (GetNewVehiclePos(v, &gp)) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3064
				/* Staying in the old tile */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3065
				if (v->u.rail.track == 0x80) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3066
					/* inside depot */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3067
					gp.x = v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3068
					gp.y = v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3069
				} else {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3070
					/* is not inside depot */
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3071
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3072
					if (!TrainCheckIfLineEnds(v)) return;
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3073
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3074
					r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3075
					if (r & 0x8) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3076
						//debug("%x & 0x8", r);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3077
						goto invalid_rail;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3078
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3079
					if (r & 0x2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3080
						TrainEnterStation(v, r >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3081
						return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3082
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3083
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3084
					if (v->current_order.type == OT_LEAVESTATION) {
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3085
						v->current_order.type = OT_NOTHING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3086
						v->current_order.flags = 0;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  3087
						InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3088
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3089
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3090
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3091
				/* A new tile is about to be entered. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3092
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3093
				byte bits;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3094
				/* Determine what direction we're entering the new tile from */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3095
				dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  3096
				enterdir = DirToDiagDir(dir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3097
				assert(enterdir==0 || enterdir==1 || enterdir==2 || enterdir==3);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3098
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3099
				/* Get the status of the tracks in the new tile and mask
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3100
				 * away the bits that aren't reachable. */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3101
				ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL) & _reachable_tracks[enterdir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3102
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3103
				/* Combine the from & to directions.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3104
				 * Now, the lower byte contains the track status, and the byte at bit 16 contains
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3105
				 * the signal status. */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3106
				tracks = ts | (ts >> 8);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3107
				bits = tracks & 0xFF;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  3108
				if ((_patches.new_pathfinding_all || _patches.yapf.rail_use_yapf) && _patches.forbid_90_deg && prev == NULL) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3109
					/* We allow wagons to make 90 deg turns, because forbid_90_deg
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3110
					 * can be switched on halfway a turn */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  3111
					bits &= ~TrackCrossesTracks(FIND_FIRST_BIT(v->u.rail.track));
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3112
				}
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3113
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3114
				if (bits == 0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3115
					//debug("%x == 0", bits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3116
					goto invalid_rail;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3117
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3118
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3119
				/* Check if the new tile contrains tracks that are compatible
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3120
				 * with the current train, if not, bail out. */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3121
				if (!CheckCompatibleRail(v, gp.new_tile)) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3122
					//debug("!CheckCompatibleRail(%p, %x)", v, gp.new_tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3123
					goto invalid_rail;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3124
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3125
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3126
				if (prev == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3127
					/* Currently the locomotive is active. Determine which one of the
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3128
					 * available tracks to choose */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3129
					chosen_track = 1 << ChooseTrainTrack(v, gp.new_tile, enterdir, bits);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3130
					assert(chosen_track & tracks);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3131
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3132
					/* Check if it's a red signal and that force proceed is not clicked. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3133
					if ( (tracks>>16)&chosen_track && v->u.rail.force_proceed == 0) goto red_light;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3134
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3135
					static byte _matching_tracks[8] = {0x30, 1, 0xC, 2, 0x30, 1, 0xC, 2};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3136
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3137
					/* The wagon is active, simply follow the prev vehicle. */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3138
					chosen_track = (byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & bits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3139
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3140
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3141
				/* make sure chosen track is a valid track */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3142
				assert(chosen_track==1 || chosen_track==2 || chosen_track==4 || chosen_track==8 || chosen_track==16 || chosen_track==32);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3143
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3144
				/* Update XY to reflect the entrance to the new tile, and select the direction to use */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3145
				{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3146
					const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3147
					gp.x = (gp.x & ~0xF) | b[0];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3148
					gp.y = (gp.y & ~0xF) | b[1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3149
					chosen_dir = b[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3150
				}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3151
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3152
				/* Call the landscape function and tell it that the vehicle entered the tile */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3153
				r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3154
				if (r & 0x8) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3155
					//debug("%x & 0x8", r);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3156
					goto invalid_rail;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3157
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3158
3657
2be38157182d (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  3159
				if (IsLevelCrossingTile(v->tile) && v->next == NULL) {
2be38157182d (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  3160
					UnbarCrossing(v->tile);
2be38157182d (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  3161
					MarkTileDirtyByTile(v->tile);
2be38157182d (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  3162
				}
2be38157182d (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  3163
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3164
				if (IsFrontEngine(v)) v->load_unload_time_rem = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3165
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3166
				if (!(r&0x4)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3167
					v->tile = gp.new_tile;
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  3168
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  3169
					if (GetTileRailType(gp.new_tile, chosen_track) != GetTileRailType(gp.old_tile, v->u.rail.track)) {
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  3170
						TrainPowerChanged(GetFirstVehicleInChain(v));
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  3171
					}
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3341
diff changeset
  3172
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3173
					v->u.rail.track = chosen_track;
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  3174
					assert(v->u.rail.track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3175
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3176
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3177
				if (IsFrontEngine(v)) TrainMovedChangeSignals(gp.new_tile, enterdir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3178
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3179
				/* Signals can only change when the first
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3180
				 * (above) or the last vehicle moves. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3181
				if (v->next == NULL)
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  3182
					TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3183
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3184
				if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3185
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3186
				v->direction = chosen_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3187
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3188
		} else {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3189
			/* in tunnel */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3190
			GetNewVehiclePos(v, &gp);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3191
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3192
			// Check if to exit the tunnel...
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3193
			if (!IsTunnelTile(gp.new_tile) ||
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3194
					!(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y)&0x4) ) {
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3195
				v->x_pos = gp.x;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3196
				v->y_pos = gp.y;
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3197
				VehiclePositionChanged(v);
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3198
				continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3199
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3200
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3201
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3202
		/* update image of train, as well as delta XY */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3203
		newdir = GetNewVehicleDirection(v, gp.x, gp.y);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3204
		UpdateTrainDeltaXY(v, newdir);
5252
6823da581c30 (svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents: 5215
diff changeset
  3205
		if (update_image) v->cur_image = GetTrainImage(v, newdir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3206
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3207
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3208
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3209
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3210
		/* update the Z position of the vehicle */
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  3211
		old_z = AfterSetTrainPos(v, (gp.new_tile != gp.old_tile));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3212
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3213
		if (prev == NULL) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3214
			/* This is the first vehicle in the train */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3215
			AffectSpeedByZChange(v, old_z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3216
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3217
	}
1438
d6bf51eff1bc (svn r1942) Fix r1938
tron
parents: 1434
diff changeset
  3218
	return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3219
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3220
invalid_rail:
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3221
	/* We've reached end of line?? */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3222
	if (prev != NULL) error("!Disconnecting train");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3223
	goto reverse_train_direction;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3224
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3225
red_light: {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3226
	/* We're in front of a red signal ?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3227
		/* find the first set bit in ts. need to do it in 2 steps, since
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3228
		 * FIND_FIRST_BIT only handles 6 bits at a time. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3229
		i = FindFirstBit2x64(ts);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3230
4632
d74da5d7683b (svn r6496) -Codechange: removed direct map access in train_cmd.c (Rubidium)
glx
parents: 4574
diff changeset
  3231
		if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3232
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3233
			v->subspeed = 0;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3234
			v->progress = 255 - 100;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3235
			if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20) return;
4632
d74da5d7683b (svn r6496) -Codechange: removed direct map access in train_cmd.c (Rubidium)
glx
parents: 4574
diff changeset
  3236
		} else if (HasSignalOnTrackdir(gp.new_tile, i)){
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3237
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3238
			v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3239
			v->progress = 255-10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3240
			if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) {
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4556
diff changeset
  3241
				TileIndex o_tile = gp.new_tile + TileOffsByDiagDir(enterdir);
1507
2d09b13f049f (svn r2011) - Fix: [ 1162209 ] Fix OS/2 build (orudge) #2
darkvater
parents: 1475
diff changeset
  3242
				VehicleAtSignalData vasd;
2d09b13f049f (svn r2011) - Fix: [ 1162209 ] Fix OS/2 build (orudge) #2
darkvater
parents: 1475
diff changeset
  3243
				vasd.tile = o_tile;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  3244
				vasd.direction = ReverseDir(dir);
1432
9086ccebb3f1 (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3245
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3246
				/* check if a train is waiting on the other side */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3247
				if (VehicleFromPos(o_tile, &vasd, CheckVehicleAtSignal) == NULL) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3248
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3249
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3250
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3251
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3252
reverse_train_direction:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3253
	v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3254
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3255
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3256
	ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3257
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3258
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1418
diff changeset
  3259
extern TileIndex CheckTunnelBusy(TileIndex tile, uint *length);
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  3260
1418
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3261
/**
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3262
 * Deletes/Clears the last wagon of a crashed train. It takes the engine of the
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3263
 * train, then goes to the last wagon and deletes that. Each call to this function
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3264
 * will remove the last wagon of a crashed train. If this wagon was on a crossing,
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3265
 * or inside a tunnel, recalculate the signals as they might need updating
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3266
 * @param v the @Vehicle of which last wagon is to be removed
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3267
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3268
static void DeleteLastWagon(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3269
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3270
	Vehicle *u = v;
1418
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3271
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3272
	/* Go to the last wagon and delete the link pointing there
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3273
	 * *u is then the one-before-last wagon, and *v the last
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3274
	 * one which will physicially be removed */
1434
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3275
	for (; v->next != NULL; v = v->next) u = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3276
	u->next = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3277
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3278
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3279
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
  3280
	RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3281
	InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3282
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3283
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3284
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3285
	DeleteVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3286
1418
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3287
	if (!(v->u.rail.track & 0xC0))
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3288
		SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(v->u.rail.track));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3289
1434
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3290
	/* Check if the wagon was on a road/rail-crossing and disable it if no
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3291
	 * others are on it */
1103
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  3292
	DisableTrainCrossing(v->tile);
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  3293
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3294
	if (v->u.rail.track == 0x40) { // inside a tunnel
1430
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1418
diff changeset
  3295
		TileIndex endtile = CheckTunnelBusy(v->tile, NULL);
68847f67a412 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1418
diff changeset
  3296
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3297
		if (endtile == INVALID_TILE) return; // tunnel is busy (error returned)
1418
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3298
1431
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3299
		switch (v->direction) {
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3300
			case 1:
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3301
			case 5:
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3302
				SetSignalsOnBothDir(v->tile, 0);
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3303
				SetSignalsOnBothDir(endtile, 0);
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3304
				break;
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3305
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3306
			case 3:
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3307
			case 7:
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3308
				SetSignalsOnBothDir(v->tile, 1);
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3309
				SetSignalsOnBothDir(endtile, 1);
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3310
				break;
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3311
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3312
			default:
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3313
				break;
93ab62952062 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3314
		}
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  3315
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3316
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3317
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3318
static void ChangeTrainDirRandomly(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3319
{
3160
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3320
	static const DirDiff delta[] = {
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3321
		DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3322
	};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3323
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3324
	do {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3325
		//I need to buffer the train direction
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3326
		if (!(v->u.rail.track & 0x40)) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3327
			v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3328
		}
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3929
diff changeset
  3329
		if (!(v->vehstatus & VS_HIDDEN)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3330
			BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3331
			UpdateTrainDeltaXY(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3332
			v->cur_image = GetTrainImage(v, v->direction);
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3333
			AfterSetTrainPos(v, false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3334
		}
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3335
	} while ((v = v->next) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3336
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3337
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3338
static void HandleCrashedTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3339
{
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3340
	int state = ++v->u.rail.crash_anim_pos;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3341
	uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3342
	Vehicle *u;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3343
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3344
	if (state == 4 && v->u.rail.track != 0x40) {
1359
8ba976aed634 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1337
diff changeset
  3345
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3346
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3347
2637
8807e6a63f89 (svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents: 2635
diff changeset
  3348
	if (state <= 200 && CHANCE16R(1, 7, r)) {
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3349
		int index = (r * 10 >> 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3350
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3351
		u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3352
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3353
			if (--index < 0) {
1137
c492b777af49 (svn r1638) -Fix: Train crashes should no longer desync the game. This is more of a
celestar
parents: 1132
diff changeset
  3354
				r = Random();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3355
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3356
				CreateEffectVehicleRel(u,
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2135
diff changeset
  3357
					GB(r,  8, 3) + 2,
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2135
diff changeset
  3358
					GB(r, 16, 3) + 2,
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2135
diff changeset
  3359
					GB(r,  0, 3) + 5,
1359
8ba976aed634 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1337
diff changeset
  3360
					EV_EXPLOSION_SMALL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3361
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3362
			}
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3363
		} while ((u = u->next) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3364
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3365
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3366
	if (state <= 240 && !(v->tick_counter & 3)) ChangeTrainDirRandomly(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3367
1128
d3ffc98b92ad (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
  3368
	if (state >= 4440 && !(v->tick_counter&0x1F)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3369
		DeleteLastWagon(v);
1128
d3ffc98b92ad (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
  3370
		InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train);
d3ffc98b92ad (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
  3371
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3372
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3373
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3374
static void HandleBrokenTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3375
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3376
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3377
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3378
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3379
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3380
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3381
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3382
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3383
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3384
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3385
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  3386
		if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  3387
			SndPlayVehicleFx((_opt.landscape != LT_CANDY) ?
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  3388
				SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  3389
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3390
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3391
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3392
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  3393
			if (u != NULL) u->u.special.unk0 = v->breakdown_delay * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3394
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3395
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3396
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3397
	if (!(v->tick_counter & 3)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3398
		if (!--v->breakdown_delay) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3399
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3400
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3401
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3402
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3403
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3404
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3405
static const byte _breakdown_speeds[16] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3406
	225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3407
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3408
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3409
static bool TrainCheckIfLineEnds(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3410
{
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  3411
	TileIndex tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3412
	uint x,y;
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3413
	uint16 break_speed;
3163
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  3414
	DiagDirection dir;
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  3415
	int t;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3416
	uint32 ts;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3417
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3418
	t = v->breakdown_ctr;
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3419
	if (t > 1) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3420
		v->vehstatus |= VS_TRAIN_SLOWING;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3421
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3422
		break_speed = _breakdown_speeds[GB(~t, 4, 4)];
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3423
		if (break_speed < v->cur_speed) v->cur_speed = break_speed;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3424
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3425
		v->vehstatus &= ~VS_TRAIN_SLOWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3426
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3427
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3428
	if (v->u.rail.track & 0x40) return true; // exit if inside a tunnel
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3429
	if (v->u.rail.track & 0x80) return true; // exit if inside a depot
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3430
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3431
	tile = v->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3432
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3433
	// tunnel entrance?
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3434
	if (IsTunnelTile(tile) &&
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3435
			DiagDirToDir(GetTunnelDirection(tile)) == v->direction) {
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3436
		return true;
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
  3437
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3438
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3439
	// depot?
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3440
	/* XXX -- When enabled, this makes it possible to crash trains of others
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3441
	     (by building a depot right against a station) */
4632
d74da5d7683b (svn r6496) -Codechange: removed direct map access in train_cmd.c (Rubidium)
glx
parents: 4574
diff changeset
  3442
/*	if (IsTileType(tile, MP_RAILWAY) && GetRailTileType(tile) == RAIL_TILE_DEPOT_WAYPOINT)
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3443
		return true;*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3444
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3445
	/* Determine the non-diagonal direction in which we will exit this tile */
3163
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  3446
	dir = DirToDiagDir(v->direction);
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  3447
	if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) {
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  3448
		dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3449
	}
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3450
	/* Calculate next tile */
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4556
diff changeset
  3451
	tile += TileOffsByDiagDir(dir);
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3452
	// determine the track status on the next tile.
3163
129a91e147b8 (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  3453
	ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[dir];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3454
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3455
	/* Calc position within the current tile ?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3456
	x = v->x_pos & 0xF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3457
	y = v->y_pos & 0xF;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3458
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3459
	switch (v->direction) {
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3460
		case DIR_N : x = ~x + ~y + 24; break;
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3461
		case DIR_NW: x = y;            /* FALLTHROUGH */
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3462
		case DIR_NE: x = ~x + 16;      break;
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3463
		case DIR_E : x = ~x + y + 8;   break;
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3464
		case DIR_SE: x = y;            break;
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3465
		case DIR_S : x = x + y - 8;    break;
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3466
		case DIR_W : x = ~y + x + 8;   break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3467
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3468
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3469
	if (GB(ts, 0, 16) != 0) {
2874
30c79e6e1c3e (svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents: 2855
diff changeset
  3470
		/* If we approach a rail-piece which we can't enter, or the back of a depot, don't enter it! */
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
  3471
		if (x + 4 >= TILE_SIZE &&
2874
30c79e6e1c3e (svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents: 2855
diff changeset
  3472
				(!CheckCompatibleRail(v, tile) ||
30c79e6e1c3e (svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents: 2855
diff changeset
  3473
				(IsTileDepotType(tile, TRANSPORT_RAIL) &&
3185
c0ef161f700f (svn r3831) Add and use GetRailDepotDirection()
tron
parents: 3184
diff changeset
  3474
				GetRailDepotDirection(tile) == dir))) {
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3475
			v->cur_speed = 0;
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3476
			ReverseTrainDirection(v);
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3477
			return false;
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3478
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3479
		if ((ts &= (ts >> 16)) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3480
			// make a rail/road crossing red
3560
4389ae5ad967 (svn r4435) - Fix: an assertion triggered when trying to remove a bridge with the remove-tool (r4348 surfaced this). In CmdRemoveRoad tiletype was not checked for ownership. Intorudce IsLevelCrossingTile() which checks if a tile is a crossing without knowing the type. Suggested by peter1138 and Tron.
Darkvater
parents: 3546
diff changeset
  3481
			if (IsLevelCrossingTile(tile)) {
3322
fa5d46929db9 (svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents: 3315
diff changeset
  3482
				if (!IsCrossingBarred(tile)) {
fa5d46929db9 (svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents: 3315
diff changeset
  3483
					BarCrossing(tile);
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  3484
					SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3485
					MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3486
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3487
			}
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3488
			return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3489
		}
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
  3490
	} else if (x + 4 >= TILE_SIZE) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3491
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3492
		ReverseTrainDirection(v);
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3493
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3494
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3495
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3496
	// slow down
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3497
	v->vehstatus |= VS_TRAIN_SLOWING;
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3498
	break_speed = _breakdown_speeds[x & 0xF];
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3499
	if (!(v->direction & 1)) break_speed >>= 1;
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3500
	if (break_speed < v->cur_speed) v->cur_speed = break_speed;
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3501
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3502
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3503
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3504
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3505
static void TrainLocoHandler(Vehicle *v, bool mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3506
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3507
	int j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3508
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3509
	/* train has crashed? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3510
	if (v->u.rail.crash_anim_pos != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3511
		if (!mode) HandleCrashedTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3512
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3513
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3514
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3515
	if (v->u.rail.force_proceed != 0) v->u.rail.force_proceed--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3516
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3517
	/* train is broken down? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3518
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3519
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3520
			HandleBrokenTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3521
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3522
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3523
		v->breakdown_ctr--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3524
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3525
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  3526
	if (HASBIT(v->u.rail.flags, VRF_REVERSING) && v->cur_speed == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3527
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3528
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3529
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3530
	/* exit if train is stopped */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3531
	if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3532
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3533
	if (ProcessTrainOrder(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3534
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3535
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3536
		v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3537
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3538
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3539
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3540
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3541
	HandleTrainLoading(v, mode);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3542
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3543
	if (v->current_order.type == OT_LOADING) return;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3544
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3545
	if (CheckTrainStayInDepot(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3546
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3547
	if (!mode) HandleLocomotiveSmokeCloud(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3548
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3549
	j = UpdateTrainSpeed(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3550
	if (j == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3551
		// if the vehicle has speed 0, update the last_speed field.
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3552
		if (v->cur_speed != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3553
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3554
		TrainCheckIfLineEnds(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3555
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3556
		do {
5252
6823da581c30 (svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents: 5215
diff changeset
  3557
			TrainController(v, true);
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  3558
			CheckTrainCollision(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3559
			if (v->cur_speed <= 0x100)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3560
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3561
		} while (--j != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3562
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3563
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3564
	SetLastSpeed(v, v->cur_speed);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3565
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3566
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3567
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3568
void Train_Tick(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3569
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3570
	if (_age_cargo_skip_counter == 0 && v->cargo_days != 0xff)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3571
		v->cargo_days++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3572
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3573
	v->tick_counter++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3574
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3575
	if (IsFrontEngine(v)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3576
		TrainLocoHandler(v, false);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3577
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3578
		// make sure vehicle wasn't deleted.
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3579
		if (v->type == VEH_Train && IsFrontEngine(v))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3580
			TrainLocoHandler(v, true);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3581
	} else if (IsFreeWagon(v) && HASBITS(v->vehstatus, VS_CRASHED)) {
1132
7378651941bf (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  3582
		// Delete flooded standalone wagon
7378651941bf (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  3583
		if (++v->u.rail.crash_anim_pos >= 4400)
7378651941bf (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  3584
			DeleteVehicle(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3585
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3586
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3587
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  3588
#define MAX_ACCEPTABLE_DEPOT_DIST 16
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  3589
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3590
static void CheckIfTrainNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3591
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  3592
	const Depot* depot;
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  3593
	TrainFindDepotData tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3594
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3595
	if (_patches.servint_trains == 0)                   return;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3596
	if (!VehicleNeedsService(v))                        return;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3597
	if (v->vehstatus & VS_STOPPED)                      return;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3598
	if (_patches.gotodepot && VehicleHasDepotOrders(v)) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3599
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3600
	// Don't interfere with a depot visit scheduled by the user, or a
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3601
	// depot visit by the order list.
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3602
	if (v->current_order.type == OT_GOTO_DEPOT &&
1530
caa16c506a22 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
  3603
			(v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3604
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3605
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3606
	if (CheckTrainIsInsideDepot(v)) {
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3607
		VehicleServiceInDepot(v);
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3608
		return;
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3609
	}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3610
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  3611
	tfdd = FindClosestTrainDepot(v, MAX_ACCEPTABLE_DEPOT_DIST);
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  3612
	/* Only go to the depot if it is not too far out of our way. */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  3613
	if (tfdd.best_length == (uint)-1 || tfdd.best_length > MAX_ACCEPTABLE_DEPOT_DIST) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3614
		if (v->current_order.type == OT_GOTO_DEPOT) {
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  3615
			/* If we were already heading for a depot but it has
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  3616
			 * suddenly moved farther away, we continue our normal
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  3617
			 * schedule? */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3618
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3619
			v->current_order.flags = 0;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  3620
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3621
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3622
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3623
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3624
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  3625
	depot = GetDepotByTile(tfdd.tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3626
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3627
	if (v->current_order.type == OT_GOTO_DEPOT &&
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  3628
			v->current_order.dest != depot->index &&
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3629
			!CHANCE16(3, 16)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3630
		return;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3631
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3632
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3633
	v->current_order.type = OT_GOTO_DEPOT;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3634
	v->current_order.flags = OF_NON_STOP;
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  3635
	v->current_order.dest = depot->index;
308
1d39f4e3eab1 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  3636
	v->dest_tile = tfdd.tile;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  3637
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3638
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3639
1790
4afb4b4e4278 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1786
diff changeset
  3640
int32 GetTrainRunningCost(const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3641
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3642
	int32 cost = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3643
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3644
	do {
540
fbed599dadbb (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
  3645
		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
2840
d68a605b2087 (svn r3388) - NewGRF: Allow train running cost class to differ from engine class. Also fix typo in r3384.
peter1138
parents: 2833
diff changeset
  3646
		if (rvi->running_cost_base > 0)
d68a605b2087 (svn r3388) - NewGRF: Allow train running cost class to differ from engine class. Also fix typo in r3384.
peter1138
parents: 2833
diff changeset
  3647
			cost += rvi->running_cost_base * _price.running_rail[rvi->running_cost_class];
4839
a8b963413d15 (svn r6765) - Fix: Don't add up running cost of articulated engine parts.
peter1138
parents: 4831
diff changeset
  3648
	} while ((v = GetNextVehicle(v)) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3649
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3650
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3651
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3652
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3653
void OnNewDay_Train(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3654
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3655
	TileIndex tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3656
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3657
	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3658
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3659
	if (IsFrontEngine(v)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3660
		CheckVehicleBreakdown(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3661
		AgeVehicle(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3662
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3663
		CheckIfTrainNeedsService(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3664
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  3665
		CheckOrders(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3666
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3667
		/* update destination */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3668
		if (v->current_order.type == OT_GOTO_STATION &&
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  3669
				(tile = GetStation(v->current_order.dest)->train_tile) != 0) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3670
			v->dest_tile = tile;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3671
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3672
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3673
		if ((v->vehstatus & VS_STOPPED) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3674
			/* running costs */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3675
			int32 cost = GetTrainRunningCost(v) / 364;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3676
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3677
			v->profit_this_year -= cost >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3678
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3679
			SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3680
			SubtractMoneyFromPlayerFract(v->owner, cost);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3681
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3682
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
1151
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  3683
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3684
		}
5518
d5923d3fa08c (svn r10372) [0.5] -Backport from trunk (r10288, r10290, r10293, r10294, r10295, r10347, r10348):
rubidium
parents: 5513
diff changeset
  3685
	} else if (IsTrainEngine(v)) {
d5923d3fa08c (svn r10372) [0.5] -Backport from trunk (r10288, r10290, r10293, r10294, r10295, r10347, r10348):
rubidium
parents: 5513
diff changeset
  3686
		/* Also age engines that aren't front engines */
d5923d3fa08c (svn r10372) [0.5] -Backport from trunk (r10288, r10290, r10293, r10294, r10295, r10347, r10348):
rubidium
parents: 5513
diff changeset
  3687
		AgeVehicle(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3688
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3689
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3690
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1087
diff changeset
  3691
void TrainsYearlyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3692
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3693
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3694
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3695
	FOR_ALL_VEHICLES(v) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3696
		if (v->type == VEH_Train && IsFrontEngine(v)) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3697
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3698
			// show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3699
			if (_patches.train_income_warn && v->owner == _local_player && v->age >= 730 && v->profit_this_year < 0) {
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  3700
				SetDParam(1, v->profit_this_year);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  3701
				SetDParam(0, v->unitnumber);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3702
				AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3703
					STR_TRAIN_IS_UNPROFITABLE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3704
					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3705
					v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3706
					0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3707
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3708
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3709
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3710
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3711
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3712
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3713
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3714
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3715
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3716
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1087
diff changeset
  3717
void InitializeTrains(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3718
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3719
	_age_cargo_skip_counter = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3720
}
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3721
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3722
/*
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3723
 * Link front and rear multiheaded engines to each other
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3724
 * This is done when loading a savegame
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3725
 */
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3726
void ConnectMultiheadedTrains(void)
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3727
{
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3728
	Vehicle *v;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3729
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3730
	FOR_ALL_VEHICLES(v) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3731
		if (v->type == VEH_Train) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3732
			v->u.rail.other_multiheaded_part = NULL;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3733
		}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3734
	}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3735
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3736
	FOR_ALL_VEHICLES(v) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3737
		if (v->type == VEH_Train && IsFrontEngine(v)) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3738
			Vehicle *u = v;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3739
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3740
			BEGIN_ENUM_WAGONS(u) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3741
				if (u->u.rail.other_multiheaded_part != NULL) continue; // we already linked this one
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3742
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3743
				if (IsMultiheaded(u)) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3744
					if (!IsTrainEngine(u)) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3745
						/* we got a rear car without a front car. We will convert it to a front one */
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3746
						SetTrainEngine(u);
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3747
						u->spritenum--;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3748
					}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3749
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3750
					{
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3751
						Vehicle *w;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3752
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  3753
						for (w = u->next; w != NULL && (w->engine_type != u->engine_type || w->u.rail.other_multiheaded_part != NULL); w = GetNextVehicle(w));
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3754
						if (w != NULL) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3755
							/* we found a car to partner with this engine. Now we will make sure it face the right way */
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3756
							if (IsTrainEngine(w)) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3757
								ClearTrainEngine(w);
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3758
								w->spritenum++;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3759
							}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3760
						}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3761
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3762
						if (w != NULL) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3763
							w->u.rail.other_multiheaded_part = u;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3764
							u->u.rail.other_multiheaded_part = w;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3765
						} else {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3766
							/* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3767
							ClearMultiheaded(u);
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3768
						}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3769
					}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3770
				}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3771
			} END_ENUM_WAGONS(u)
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3772
		}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3773
	}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3774
}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3775
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3776
/*
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3777
 *  Converts all trains to the new subtype format introduced in savegame 16.2
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3778
 *  It also links multiheaded engines or make them forget they are multiheaded if no suitable partner is found
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3779
 */
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3780
void ConvertOldMultiheadToNew(void)
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3781
{
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3782
	Vehicle *v;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3783
	FOR_ALL_VEHICLES(v) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3784
		if (v->type == VEH_Train) {
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  3785
			SETBIT(v->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3786
		}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3787
	}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3788
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3789
	FOR_ALL_VEHICLES(v) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3790
		if (v->type == VEH_Train) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3791
			if (HASBIT(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3792
				Vehicle *u = v;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3793
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3794
				BEGIN_ENUM_WAGONS(u) {
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3795
					const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3796
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3797
					CLRBIT(u->subtype, 7);
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3798
					switch (u->subtype) {
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  3799
						case 0: /* TS_Front_Engine */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3800
							if (rvi->flags & RVI_MULTIHEAD) SetMultiheaded(u);
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3801
							SetFrontEngine(u);
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3802
							SetTrainEngine(u);
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3803
							break;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3804
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  3805
						case 1: /* TS_Artic_Part */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3806
							u->subtype = 0;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3807
							SetArticulatedPart(u);
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3808
							break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3809
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  3810
						case 2: /* TS_Not_First */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3811
							u->subtype = 0;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3812
							if (rvi->flags & RVI_WAGON) {
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3813
								// normal wagon
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3814
								SetTrainWagon(u);
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3815
								break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3816
							}
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3817
							if (rvi->flags & RVI_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3818
								// rear end of a multiheaded engine
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3819
								SetMultiheaded(u);
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3820
								break;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3821
							}
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3822
							if (rvi->flags & RVI_MULTIHEAD) SetMultiheaded(u);
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3823
							SetTrainEngine(u);
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3824
							break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3825
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  3826
						case 4: /* TS_Free_Car */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3827
							u->subtype = 0;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3828
							SetTrainWagon(u);
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3829
							SetFreeWagon(u);
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3830
							break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3831
						default: NOT_REACHED(); break;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3832
					}
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3008
diff changeset
  3833
				} END_ENUM_WAGONS(u)
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3834
			}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3835
		}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3836
	}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3837
}