src/train_cmd.cpp
author frosch
Tue, 01 Apr 2008 14:03:20 +0000
changeset 9289 92f1249c304e
parent 9282 2bb9703aeb39
child 9290 f14a58dd8837
permissions -rw-r--r--
(svn r12531) -Codechange: Rename some variables for consistency.
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
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
     3
/** @file train_cmd.cpp */
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1883
diff changeset
     6
#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
     7
#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
     8
#include "debug.h"
8615
6b91ca653bad (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8612
diff changeset
     9
#include "tile_cmd.h"
6949
72d11a1e1e60 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6918
diff changeset
    10
#include "landscape.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
    11
#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
    12
#include "station_map.h"
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
    13
#include "tunnel_map.h"
7476
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7462
diff changeset
    14
#include "timetable.h"
7268
9c77f6baef3b (svn r10008) -Codechange: Move a couple of functions related to articulated vehicles to a file of their own.
maedhros
parents: 7267
diff changeset
    15
#include "articulated_vehicles.h"
8612
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8610
diff changeset
    16
#include "command_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include "pathfind.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    18
#include "npf.h"
9281
d8cd9ac52a68 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 9280
diff changeset
    19
#include "station_base.h"
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9208
diff changeset
    20
#include "news_func.h"
9282
2bb9703aeb39 (svn r12490) -Codechange: rename engine.h to engine_func.h and remove unneeded inclusions of engine.h and/or replace them with engine_type.h.
rubidium
parents: 9281
diff changeset
    21
#include "engine_func.h"
8750
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8744
diff changeset
    22
#include "player_func.h"
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8744
diff changeset
    23
#include "player_base.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"
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
    28
#include "bridge.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
    29
#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
    30
#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
    31
#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
    32
#include "newgrf_text.h"
8596
27646407e0bc (svn r11661) -Codechange: some header reworks in order to try to reduce the compile time of OpenTTD by reduce the amount of circular-ish dependencies.
rubidium
parents: 8584
diff changeset
    33
#include "direction_func.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
    34
#include "yapf/yapf.h"
6417
26acff62d001 (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 6324
diff changeset
    35
#include "cargotype.h"
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
    36
#include "group.h"
8298
b8f2b501e23a (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 8279
diff changeset
    37
#include "table/sprites.h"
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8577
diff changeset
    38
#include "tunnelbridge_map.h"
8610
17cc343a23dd (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8604
diff changeset
    39
#include "strings_func.h"
8627
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8615
diff changeset
    40
#include "functions.h"
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8615
diff changeset
    41
#include "window_func.h"
8636
2b158acb649c (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8635
diff changeset
    42
#include "date_func.h"
8640
1e93b81e96d2 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8639
diff changeset
    43
#include "vehicle_func.h"
8653
a83f7a536919 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8641
diff changeset
    44
#include "sound_func.h"
8734
b7fc8c6fd379 (svn r11802) -Fix [FS#716]: do not crash trains when leaving depot to a very long track
smatz
parents: 8732
diff changeset
    45
#include "signal_func.h"
8707
55835d8fbfcd (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8693
diff changeset
    46
#include "variables.h"
8708
0c29fbc79be4 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8707
diff changeset
    47
#include "autoreplace_gui.h"
8720
4e60c30e2006 (svn r11787) -Codechange: more header rewrites. This time related to viewport.h.
rubidium
parents: 8708
diff changeset
    48
#include "gfx_func.h"
8766
c86cfa3a7580 (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8763
diff changeset
    49
#include "settings_type.h"
9280
9c03416d26b1 (svn r12488) -Codechange: split order.h into order_base.h and order_func.h.
rubidium
parents: 9277
diff changeset
    50
#include "order_func.h"
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8577
diff changeset
    51
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8754
diff changeset
    52
#include "table/strings.h"
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8754
diff changeset
    53
#include "table/train_cmd.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
    55
static bool TrainCheckIfLineEnds(Vehicle *v);
9206
12d35b56014c (svn r12384) -Fix (r2428): do not disconnect train when reversing, it may 'think' it is whole in a depot
smatz
parents: 9202
diff changeset
    56
static void TrainController(Vehicle *v, Vehicle *nomove, bool update_image);
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
    57
static TileIndex TrainApproachingCrossingTile(const Vehicle *v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
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
    59
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
    60
static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
8744
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    61
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    62
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    63
/**
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    64
 * Determine the side in which the train will leave the tile
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    65
 *
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    66
 * @param direction vehicle direction
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    67
 * @param track vehicle track bits
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    68
 * @return side of tile the train will leave
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    69
 */
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    70
static inline DiagDirection TrainExitDir(Direction direction, TrackBits track)
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    71
{
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    72
	static const TrackBits state_dir_table[DIAGDIR_END] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER };
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    73
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    74
	DiagDirection diagdir = DirToDiagDir(direction);
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    75
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    76
	/* Determine the diagonal direction in which we will exit this tile */
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    77
	if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) {
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    78
		diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    79
	}
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    80
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    81
	return diagdir;
f926fa3978c8 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8734
diff changeset
    82
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
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
    84
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
    85
/** 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
    86
 * @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
    87
 * @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
    88
 */
5316
b04421921eae (svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents: 5303
diff changeset
    89
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
    90
{
6440
14530751458a (svn r8850) -Codechange: Use the cargo type's is_freight flag instead of checking the cargo type to determine if the cargo type is freight cargo type cargo type.
peter1138
parents: 6417
diff changeset
    91
	if (!GetCargo(cargo)->is_freight) 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
    92
	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
    93
}
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
    94
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
    95
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
    96
/**
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
    97
 * Recalculates the cached total power of a train. Should be called when the consist is changed
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
    98
 * @param v First vehicle of the consist.
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
    99
 */
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   100
void TrainPowerChanged(Vehicle* v)
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   101
{
6986
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   102
	uint32 total_power = 0;
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   103
	uint32 max_te = 0;
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   104
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
   105
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   106
		/* Power is not added for articulated parts */
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   107
		if (IsArticulatedPart(u)) continue;
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   108
6498
aff910a05c6e (svn r8935) -Codechange: unification of track type between road and rail tiles, unification of ground type between normal rail tiles and depots/waypoints and removing the need for RailTileSubType.
rubidium
parents: 6481
diff changeset
   109
		RailType railtype = GetRailType(u->tile);
6020
f8008c438f82 (svn r8320) -Fix
tron
parents: 5984
diff changeset
   110
		bool engine_has_power = HasPowerOnRail(u->u.rail.railtype, railtype);
f8008c438f82 (svn r8320) -Fix
tron
parents: 5984
diff changeset
   111
		bool wagon_has_power  = HasPowerOnRail(v->u.rail.railtype, railtype);
f8008c438f82 (svn r8320) -Fix
tron
parents: 5984
diff changeset
   112
f8008c438f82 (svn r8320) -Fix
tron
parents: 5984
diff changeset
   113
		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   114
6986
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   115
		if (engine_has_power) {
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   116
			uint16 power = GetVehicleProperty(u, 0x0B, rvi_u->power);
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   117
			if (power != 0) {
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   118
				total_power += power;
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   119
				/* Tractive effort in (tonnes * 1000 * 10 =) N */
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   120
				max_te += (u->u.rail.cached_veh_weight * 10000 * GetVehicleProperty(u, 0x1F, rvi_u->tractive_effort)) / 256;
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   121
			}
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   122
		}
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   123
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   124
		if (HasBit(u->u.rail.flags, VRF_POWEREDWAGON) && (wagon_has_power)) {
6986
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   125
			total_power += RailVehInfo(u->u.rail.first_engine)->pow_wag_power;
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   126
		}
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   127
	}
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   128
6986
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   129
	if (v->u.rail.cached_power != total_power || v->u.rail.cached_max_te != max_te) {
8035
a0200ced6d9f (svn r11059) -Fix [FS#1182]: inconsistency between Rail<->ElRail conversions of different kinds of rail containing tiles (normal rail, stations, depots, etc). Patch by SmatZ.
rubidium
parents: 8025
diff changeset
   130
		/* If it has no power (no catenary), stop the train */
a0200ced6d9f (svn r11059) -Fix [FS#1182]: inconsistency between Rail<->ElRail conversions of different kinds of rail containing tiles (normal rail, stations, depots, etc). Patch by SmatZ.
rubidium
parents: 8025
diff changeset
   131
		if (total_power == 0) v->vehstatus |= VS_STOPPED;
a0200ced6d9f (svn r11059) -Fix [FS#1182]: inconsistency between Rail<->ElRail conversions of different kinds of rail containing tiles (normal rail, stations, depots, etc). Patch by SmatZ.
rubidium
parents: 8025
diff changeset
   132
6986
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   133
		v->u.rail.cached_power = total_power;
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   134
		v->u.rail.cached_max_te = max_te;
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   135
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
   136
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   137
	}
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   138
}
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   139
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   140
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   141
/**
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
   142
 * 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
   143
 * 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
   144
 * @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
   145
 */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2704
diff changeset
   146
static void TrainCargoChanged(Vehicle* v)
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   147
{
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
   148
	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
   149
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
   150
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
7506
e52d89f5c7c1 (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7498
diff changeset
   151
		uint32 vweight = GetCargo(u->cargo_type)->weight * 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
   152
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   153
		/* 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
   154
		if (!IsArticulatedPart(u)) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   155
			/* vehicle weight is the sum of the weight of the vehicle and the weight of its cargo */
7068
846986cf3a6e (svn r9780) -Codechange: [NewGRF] Add support for variable vehicle weight for trains (callback 36, property 16)
peter1138
parents: 7061
diff changeset
   156
			vweight += GetVehicleProperty(u, 0x16, RailVehInfo(u->engine_type)->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
   157
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   158
			/* powered wagons have extra weight added */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   159
			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
   160
				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
   161
		}
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
   162
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   163
		/* consist weight is the sum of the weight of all vehicles in the consist */
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
   164
		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
   165
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   166
		/* store vehicle weight in cache */
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
   167
		u->u.rail.cached_veh_weight = vweight;
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   168
	}
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
   169
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   170
	/* store consist weight in cache */
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
   171
	v->u.rail.cached_weight = weight;
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   172
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   173
	/* Now update train power (tractive effort is dependent on weight) */
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   174
	TrainPowerChanged(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
   175
}
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
   176
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
   177
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
   178
/**
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
   179
 * 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
   180
 * 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
   181
 * 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
   182
 * @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
   183
 */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   184
void TrainConsistChanged(Vehicle* v)
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   185
{
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
   186
	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
   187
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   188
	assert(v->type == VEH_TRAIN);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   189
	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
   190
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   191
	const RailVehicleInfo *rvi_v = RailVehInfo(v->engine_type);
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   192
	EngineID 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
   193
	v->u.rail.cached_total_length = 0;
8732
b18f578f7c16 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8726
diff changeset
   194
	v->u.rail.compatible_railtypes = RAILTYPES_NONE;
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
   195
8674
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   196
	bool train_can_tilt = true;
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   197
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
   198
	for (Vehicle *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
   199
		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   200
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   201
		/* Check the v->first cache. */
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   202
		assert(u->First() == v);
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
   203
8674
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   204
		if (!HasBit(EngInfo(u->engine_type)->misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   205
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   206
		/* update the 'first engine' */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   207
		u->u.rail.first_engine = v == u ? INVALID_ENGINE : first_engine;
6074
e70d63ef4d62 (svn r8385) -Fix
tron
parents: 6020
diff changeset
   208
		u->u.rail.railtype = rvi_u->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
   209
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
   210
		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
   211
7099
1528b11097af (svn r9818) -Codechange: Cache wagon override sprite set whenever consist is changed, instead of 'recalculating' it every time it is used or just tested.
peter1138
parents: 7090
diff changeset
   212
		/* Cache wagon override sprite group. NULL is returned if there is none */
1528b11097af (svn r9818) -Codechange: Cache wagon override sprite set whenever consist is changed, instead of 'recalculating' it every time it is used or just tested.
peter1138
parents: 7090
diff changeset
   213
		u->u.rail.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->u.rail.first_engine);
1528b11097af (svn r9818) -Codechange: Cache wagon override sprite set whenever consist is changed, instead of 'recalculating' it every time it is used or just tested.
peter1138
parents: 7090
diff changeset
   214
8298
b8f2b501e23a (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 8279
diff changeset
   215
		/* Reset color map */
b8f2b501e23a (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 8279
diff changeset
   216
		u->colormap = PAL_NONE;
b8f2b501e23a (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 8279
diff changeset
   217
9199
39147e16a5a0 (svn r12377) -Fix [FS#1854]: set cached value for vehicle property 25 before other cached values
glx
parents: 9192
diff changeset
   218
		/* Set user defined data (must be done before other properties) */
39147e16a5a0 (svn r12377) -Fix [FS#1854]: set cached value for vehicle property 25 before other cached values
glx
parents: 9192
diff changeset
   219
		u->u.rail.user_def_data = GetVehicleProperty(u, 0x25, rvi_u->user_def_data);
39147e16a5a0 (svn r12377) -Fix [FS#1854]: set cached value for vehicle property 25 before other cached values
glx
parents: 9192
diff changeset
   220
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   221
		if (rvi_u->visual_effect != 0) {
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   222
			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
   223
		} else {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   224
			if (IsTrainWagon(u) || IsArticulatedPart(u)) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   225
				/* 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
   226
				u->u.rail.cached_vis_effect = 0x40;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   227
			} else if (rvi_u->engclass == 0) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   228
				/* Steam is offset by -4 units */
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   229
				u->u.rail.cached_vis_effect = 4;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   230
			} else {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   231
				/* Diesel fumes and sparks come from the centre */
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   232
				u->u.rail.cached_vis_effect = 8;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   233
			}
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   234
		}
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   235
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   236
		if (!IsArticulatedPart(u)) {
3926
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   237
			/* Check powered wagon / visual effect callback */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   238
			if (HasBit(EngInfo(u->engine_type)->callbackmask, CBM_TRAIN_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
   239
				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
   240
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   241
				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
   242
			}
eb6d2e32fc79 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   243
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
   244
			if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON &&
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   245
				UsesWagonOverride(u) && !HasBit(u->u.rail.cached_vis_effect, 7)) {
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
   246
				/* wagon is powered */
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
   247
				SetBit(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   248
			} else {
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
   249
				ClrBit(u->u.rail.flags, VRF_POWEREDWAGON);
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   250
			}
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   251
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
   252
			/* 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
   253
			   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
   254
			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
   255
				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
   256
			}
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
   257
5116
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   258
			/* 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
   259
			 * existing electric engines when elrails are disabled and then re-enabled */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   260
			if (HasBit(u->u.rail.flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL)) {
5116
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   261
				u->u.rail.railtype = RAILTYPE_RAIL;
8732
b18f578f7c16 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8726
diff changeset
   262
				u->u.rail.compatible_railtypes |= RAILTYPES_RAIL;
5116
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   263
			}
2a33a74925c5 (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   264
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   265
			/* max speed is the minimum of the speed limits of all vehicles in the consist */
6986
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   266
			if ((rvi_u->railveh_type != RAILVEH_WAGON || _patches.wagon_speed_limits) && !UsesWagonOverride(u)) {
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   267
				uint16 speed = GetVehicleProperty(u, 0x09, rvi_u->max_speed);
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   268
				if (speed != 0) max_speed = min(speed, max_speed);
168d3add1f13 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6980
diff changeset
   269
			}
1908
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   270
		}
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   271
7140
422457e07b54 (svn r9875) -Fix (r9828): Only set carriage capacity if the wagon has not been refitted.
peter1138
parents: 7139
diff changeset
   272
		if (u->cargo_type == rvi_u->cargo_type && u->cargo_subtype == 0) {
422457e07b54 (svn r9875) -Fix (r9828): Only set carriage capacity if the wagon has not been refitted.
peter1138
parents: 7139
diff changeset
   273
			/* Set cargo capacity if we've not been refitted */
422457e07b54 (svn r9875) -Fix (r9828): Only set carriage capacity if the wagon has not been refitted.
peter1138
parents: 7139
diff changeset
   274
			u->cargo_cap = GetVehicleProperty(u, 0x14, rvi_u->capacity);
422457e07b54 (svn r9875) -Fix (r9828): Only set carriage capacity if the wagon has not been refitted.
peter1138
parents: 7139
diff changeset
   275
		}
7104
59f24cd8470b (svn r9828) -Codechange: [NewGRF] Add support for changing cargo capacity with callback 36. This is set on construction for ships and roadvehicles, and whenever carriages are attached for trains.
peter1138
parents: 7099
diff changeset
   276
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   277
		/* check the vehicle length (callback) */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   278
		uint16 veh_len = CALLBACK_FAILED;
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   279
		if (HasBit(EngInfo(u->engine_type)->callbackmask, CBM_VEHICLE_LENGTH)) {
7711
66dffbd3b136 (svn r10493) -Codechange: update some callback ID enums to reflect their changed usage, add a few and update the comments.
rubidium
parents: 7692
diff changeset
   280
			veh_len = GetVehicleCallback(CBID_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
   281
		}
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
   282
		if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8377
diff changeset
   283
		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
   284
		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
   285
		v->u.rail.cached_total_length += u->u.rail.cached_veh_length;
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   286
	}
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
   287
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   288
	/* store consist weight/max speed in cache */
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
   289
	v->u.rail.cached_max_speed = max_speed;
8674
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   290
	v->u.rail.cached_tilt = train_can_tilt;
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
   291
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   292
	/* recalculate cached weights and power too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added) */
1908
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   293
	TrainCargoChanged(v);
9200
8daa2f627bdb (svn r12378) -Fix: refresh vehicle details window when cached values are updated
glx
parents: 9199
diff changeset
   294
9202
6c1679b46549 (svn r12380) -Fix: update train acceleration and max speed after setting cached value to ensure the correct max speed is used with disabled real acceleration
glx
parents: 9201
diff changeset
   295
	if (IsFrontEngine(v)) {
6c1679b46549 (svn r12380) -Fix: update train acceleration and max speed after setting cached value to ensure the correct max speed is used with disabled real acceleration
glx
parents: 9201
diff changeset
   296
		UpdateTrainAcceleration(v);
6c1679b46549 (svn r12380) -Fix: update train acceleration and max speed after setting cached value to ensure the correct max speed is used with disabled real acceleration
glx
parents: 9201
diff changeset
   297
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
6c1679b46549 (svn r12380) -Fix: update train acceleration and max speed after setting cached value to ensure the correct max speed is used with disabled real acceleration
glx
parents: 9201
diff changeset
   298
	}
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
   299
}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   300
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   301
enum AccelType {
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   302
	AM_ACCEL,
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   303
	AM_BRAKE
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   304
};
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   305
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   306
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
   307
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   308
	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
   309
	StationID sid = GetStationIndex(tile);
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   310
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   311
	assert(v->type == VEH_TRAIN);
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   312
	/* When does a train drive through a station
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   313
	 * first we deal with the "new nonstop handling" */
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
   314
	if (_patches.new_nonstop && o->flags & OFB_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
   315
		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
   316
	}
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   317
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   318
	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
   319
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
   320
	if (sid != o->dest && (o->flags & OFB_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
   321
		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
   322
	}
1236
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   323
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   324
	return true;
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   325
}
394a1b3d6f3e (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   326
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   327
/** new acceleration*/
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   328
static int GetTrainAcceleration(Vehicle *v, bool mode)
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   329
{
8665
758b79eb2bdc (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138
parents: 8664
diff changeset
   330
	static const int absolute_max_speed = 2000;
758b79eb2bdc (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138
parents: 8664
diff changeset
   331
	int max_speed = absolute_max_speed;
8057
624ab0243ea2 (svn r11086) -Fix/Revert (r11053, r10984): "fixing" r11053 made the acceleration code use the wrong speed, which can be fixed by reverting a small piece of r10984.
rubidium
parents: 8047
diff changeset
   332
	int speed = v->cur_speed * 10 / 16; // km-ish/h -> mp/h
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   333
	int curvecount[2] = {0, 0};
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   334
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   335
	/*first find the curve speed limit */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   336
	int numcurve = 0;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   337
	int sum = 0;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   338
	int pos = 0;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   339
	int lastpos = -1;
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
   340
	for (const Vehicle *u = v; u->Next() != NULL; u = u->Next(), pos++) {
8671
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   341
		Direction this_dir = u->direction;
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   342
		Direction next_dir = u->Next()->direction;
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   343
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   344
		DirDiff dirdiff = DirDifference(this_dir, next_dir);
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   345
		if (dirdiff == DIRDIFF_SAME) continue;
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   346
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   347
		if (dirdiff == DIRDIFF_45LEFT) curvecount[0]++;
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   348
		if (dirdiff == DIRDIFF_45RIGHT) curvecount[1]++;
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   349
		if (dirdiff == DIRDIFF_45LEFT || dirdiff == DIRDIFF_45RIGHT) {
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   350
			if (lastpos != -1) {
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   351
				numcurve++;
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   352
				sum += pos - lastpos;
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   353
				if (pos - lastpos == 1) {
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   354
					max_speed = 88;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   355
				}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   356
			}
8671
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   357
			lastpos = pos;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   358
		}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   359
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   360
		/*if we have a 90 degree turn, fix the speed limit to 60 */
8671
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   361
		if (dirdiff == DIRDIFF_90LEFT || dirdiff == DIRDIFF_90RIGHT) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   362
			max_speed = 61;
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
	}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   365
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   366
	if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   367
		int total = curvecount[0] + curvecount[1];
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   368
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   369
		if (curvecount[0] == 1 && curvecount[1] == 1) {
8665
758b79eb2bdc (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138
parents: 8664
diff changeset
   370
			max_speed = absolute_max_speed;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   371
		} else if (total > 1) {
8671
c4f636185eb9 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8666
diff changeset
   372
			if (numcurve > 0) sum /= numcurve;
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8377
diff changeset
   373
			max_speed = 232 - (13 - Clamp(sum, 1, 12)) * (13 - Clamp(sum, 1, 12));
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   374
		}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   375
	}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   376
8665
758b79eb2bdc (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138
parents: 8664
diff changeset
   377
	if (max_speed != absolute_max_speed) {
758b79eb2bdc (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138
parents: 8664
diff changeset
   378
		/* Apply the engine's rail type curve speed advantage, if it slowed by curves */
758b79eb2bdc (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138
parents: 8664
diff changeset
   379
		const RailtypeInfo *rti = GetRailTypeInfo(v->u.rail.railtype);
758b79eb2bdc (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138
parents: 8664
diff changeset
   380
		max_speed += (max_speed / 2) * rti->curve_speed;
8674
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   381
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   382
		if (v->u.rail.cached_tilt) {
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   383
			/* Apply max_speed bonus of 20% for a tilting train */
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   384
			max_speed += max_speed / 5;
6dc3d2a35da0 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138
parents: 8671
diff changeset
   385
		}
8665
758b79eb2bdc (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138
parents: 8664
diff changeset
   386
	}
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   387
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   388
	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
   389
		if (TrainShouldStop(v, v->tile)) {
6324
ebf3649bd88f (svn r8709) -Fix/Codechange: Rename the function GetStationPlatforms into GetPlatformLength because that is what it really does. Overload it because there is already a GetPlatformLength (one gives the length of the whole platform, the other gives the remaining length in a given direction). Turned both functions into methods of Station. While messing around with it, fix a problem where loading times for overhanging trains are miscomputed.
celestar
parents: 6320
diff changeset
   390
			int station_length = GetStationByTile(v->tile)->GetPlatformLength(v->tile, DirToDiagDir(v->direction));
8666
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   391
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   392
			int st_max_speed = 120;
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   393
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   394
			int delta_v = v->cur_speed / (station_length + 1);
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   395
			if (v->max_speed > (v->cur_speed - delta_v)) {
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   396
				st_max_speed = v->cur_speed - (delta_v / 10);
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   397
			}
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   398
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   399
			st_max_speed = max(st_max_speed, 25 * station_length);
ce3e587535c3 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8665
diff changeset
   400
			max_speed = min(max_speed, st_max_speed);
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   401
		}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   402
	}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   403
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   404
	int mass = v->u.rail.cached_weight;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   405
	int power = v->u.rail.cached_power * 746;
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
   406
	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
   407
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   408
	int num = 0; //number of vehicles, change this into the number of axles later
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   409
	int incl = 0;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   410
	int drag_coeff = 20; //[1e-4]
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
   411
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   412
		num++;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   413
		drag_coeff += 3;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   414
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
   415
		if (u->u.rail.track == TRACK_BIT_DEPOT) max_speed = min(max_speed, 61);
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   416
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   417
		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
   418
			incl += u->u.rail.cached_veh_weight * 60; //3% slope, quite a bit actually
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   419
		} 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
   420
			incl -= u->u.rail.cached_veh_weight * 60;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   421
		}
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
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   424
	v->max_speed = max_speed;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   425
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   426
	const int area = 120;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   427
	const int friction = 35; //[1e-3]
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   428
	int resistance;
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   429
	if (v->u.rail.railtype != RAILTYPE_MAGLEV) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   430
		resistance = 13 * mass / 10;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   431
		resistance += 60 * num;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   432
		resistance += friction * mass * speed / 1000;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   433
		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
   434
	} else {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   435
		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
   436
	}
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   437
	resistance += incl;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   438
	resistance *= 4; //[N]
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   439
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
   440
	/* 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
   441
	 * actually double the train's power */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   442
	const int max_te = v->u.rail.cached_max_te; // [N]
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   443
	int force;
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
   444
	if (speed > 2) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   445
		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
   446
			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
   447
			case RAILTYPE_ELECTRIC:
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   448
			case RAILTYPE_MONO:
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   449
				force = power / speed; //[N]
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   450
				force *= 22;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   451
				force /= 10;
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   452
				if (mode == AM_ACCEL && force > max_te) force = max_te;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   453
				break;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   454
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   455
			default: NOT_REACHED();
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   456
			case RAILTYPE_MAGLEV:
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   457
				force = power / 25;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   458
				break;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   459
		}
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   460
	} else {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   461
		/* "kickoff" acceleration */
5588
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   462
		force = (mode == AM_ACCEL && v->u.rail.railtype != RAILTYPE_MAGLEV) ? min(max_te, power) : power;
1bcb6b4c01d8 (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5573
diff changeset
   463
		force = max(force, (mass * 8) + resistance);
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   464
	}
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   465
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   466
	if (force <= 0) force = 10000;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   467
2519
67a2d670c17e (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   468
	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
   469
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   470
	if (mode == AM_ACCEL) {
1684
d09bc0beec4c (svn r2188) Revert r2185, because trains always accerlated (Requested by Celestar)
tron
parents: 1683
diff changeset
   471
		return (force - resistance) / (mass * 4);
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   472
	} else {
5279
658a16e02195 (svn r7425) -Fix (r1681): With realistic acceleration, guarantee a minimum braking
peter1138
parents: 5277
diff changeset
   473
		return min((-force - resistance) / (mass * 4), -10000 / (mass * 4));
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   474
	}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   475
}
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   476
9192
7e4b0b777375 (svn r12369) -Fix (r1681): reset train speed limits when _patches.realistic_acceleration changes
smatz
parents: 9191
diff changeset
   477
void UpdateTrainAcceleration(Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
{
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   479
	assert(IsFrontEngine(v));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
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
   481
	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
   482
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   483
	uint power = v->u.rail.cached_power;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   484
	uint weight = v->u.rail.cached_weight;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
	assert(weight != 0);
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8377
diff changeset
   486
	v->acceleration = Clamp(power / weight * 4, 1, 255);
0
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
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7605
diff changeset
   489
int Train::GetImage(Direction direction) const
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
{
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7605
diff changeset
   491
	int img = this->spritenum;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
	int base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   494
	if (HasBit(this->u.rail.flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
   495
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
	if (is_custom_sprite(img)) {
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7605
diff changeset
   497
		base = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   498
		if (base != 0) return base;
8722
30cffd0c4e40 (svn r11789) -Codechange: coding style for global variable names
peter1138
parents: 8720
diff changeset
   499
		img = _orig_rail_vehicle_info[this->engine_type].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   501
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   502
	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
   503
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7605
diff changeset
   504
	if (this->cargo.Count() >= this->cargo_cap / 2U) base += _wagon_full_adder[img];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
	return base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   508
void DrawTrainEngine(int x, int y, EngineID engine, SpriteID pal)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
{
540
fbed599dadbb (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   510
	const RailVehicleInfo *rvi = RailVehInfo(engine);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   511
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
	int img = rvi->image_index;
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   513
	SpriteID image = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   515
	if (is_custom_sprite(img)) {
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
   516
		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
   517
		if (image == 0) {
8722
30cffd0c4e40 (svn r11789) -Codechange: coding style for global variable names
peter1138
parents: 8720
diff changeset
   518
			img = _orig_rail_vehicle_info[engine].image_index;
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
   519
		} 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
   520
			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
   521
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
	}
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   523
	if (image == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
		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
   525
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
   527
	if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   528
		DrawSprite(image, pal, x - 14, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
		x += 15;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
		image = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
		if (is_custom_sprite(img)) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   532
			image = GetCustomVehicleIcon(engine, DIR_E);
8722
30cffd0c4e40 (svn r11789) -Codechange: coding style for global variable names
peter1138
parents: 8720
diff changeset
   533
			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
   534
		}
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   535
		if (image == 0) {
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   536
			image =
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   537
				((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
   538
				_engine_sprite_base[img + 1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
	}
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   541
	DrawSprite(image, pal, x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
   544
static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
{
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   546
	const RailVehicleInfo *rvi = RailVehInfo(engine);
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8722
diff changeset
   547
	CommandCost value(EXPENSES_NEW_VEHICLES, (GetEngineProperty(engine, 0x17, rvi->base_cost) * _price.build_railwagon) >> 8);
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   548
8091
f24d360b6ba1 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 8089
diff changeset
   549
	uint num_vehicles = 1 + CountArticulatedParts(engine, false);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   550
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
	if (!(flags & DC_QUERY_COST)) {
9277
c57fc7171a46 (svn r12482) -Fix: [build train] we don't need to have a depot in order to just check the price of a rail vehicle so don't check for compatible rails on the tile either
bjarni
parents: 9259
diff changeset
   552
		/* Check that the wagon can drive on the track in question */
c57fc7171a46 (svn r12482) -Fix: [build train] we don't need to have a depot in order to just check the price of a rail vehicle so don't check for compatible rails on the tile either
bjarni
parents: 9259
diff changeset
   553
		if (!IsCompatibleRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
c57fc7171a46 (svn r12482) -Fix: [build train] we don't need to have a depot in order to just check the price of a rail vehicle so don't check for compatible rails on the tile either
bjarni
parents: 9259
diff changeset
   554
8091
f24d360b6ba1 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 8089
diff changeset
   555
		/* Allow for the wagon and the articulated parts, plus one to "terminate" the list. */
f24d360b6ba1 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 8089
diff changeset
   556
		Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
f24d360b6ba1 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 8089
diff changeset
   557
		memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
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
   558
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7867
diff changeset
   559
		if (!Vehicle::AllocateList(vl, num_vehicles))
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   560
			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
   561
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
		if (flags & DC_EXEC) {
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   563
			Vehicle *v = vl[0];
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   564
			v->spritenum = rvi->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   566
			Vehicle *u = NULL;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   567
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   568
			Vehicle *w;
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   569
			FOR_ALL_VEHICLES(w) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   570
				if (w->type == VEH_TRAIN && w->tile == tile &&
8192
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
   571
				    IsFreeWagon(w) && w->engine_type == engine &&
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
   572
				    !HASBITS(w->vehstatus, VS_CRASHED)) {          /// do not connect new wagon with crashed/flooded consists
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   573
					u = GetLastVehicleInChain(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
8278
c10ecabf001d (svn r11332) -Fix: vehicles getting a value of 0 on construction.
rubidium
parents: 8192
diff changeset
   578
			v = new (v) Train();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
			v->engine_type = engine;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   581
			DiagDirection dir = GetRailDepotDirection(tile);
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   582
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   583
			v->direction = DiagDirToDir(dir);
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   584
			v->tile = tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   585
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   586
			int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   587
			int 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
   588
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
			v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
			v->y_pos = y;
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   591
			v->z_pos = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
			v->owner = _current_player;
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
   593
			v->u.rail.track = TRACK_BIT_DEPOT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
			v->vehstatus = VS_HIDDEN | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   596
			v->subtype = 0;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   597
			SetTrainWagon(v);
7267
f49300df3842 (svn r10007) -Codechange: Add some asserts to IsFrontEngine and friends to ensure that only trains use them.
maedhros
parents: 7248
diff changeset
   598
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
			if (u != NULL) {
7989
116c8f9769af (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7988
diff changeset
   600
				u->SetNext(v);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   601
			} else {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   602
				SetFreeWagon(v);
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
   603
				InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
			}
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
			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
   607
			v->cargo_subtype = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
			v->cargo_cap = rvi->capacity;
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   609
			v->value = value.GetCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
//			v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
6074
e70d63ef4d62 (svn r8385) -Fix
tron
parents: 6020
diff changeset
   612
			v->u.rail.railtype = rvi->railtype;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   613
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   614
			v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
			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
   616
			v->random_bits = VehicleRandomBits();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   617
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
   618
			v->group_id = DEFAULT_GROUP;
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
   619
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7285
diff changeset
   620
			AddArticulatedParts(vl, VEH_TRAIN);
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   621
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   622
			_new_vehicle_id = v->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
			VehiclePositionChanged(v);
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   625
			TrainConsistChanged(v->First());
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   626
			UpdateTrainGroupID(v->First());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
2970
77fa46c313c0 (svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents: 2962
diff changeset
   629
			if (IsLocalPlayer()) {
7921
f99d5c351e75 (svn r10823) -Fix r8610 [FS#1097]: the autoreplace window vehicle count didn't always update correctly) (Matthias)
bjarni
parents: 7894
diff changeset
   630
				InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
2970
77fa46c313c0 (svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents: 2962
diff changeset
   631
			}
6195
b90cf92697b9 (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 6173
diff changeset
   632
			GetPlayer(_current_player)->num_engines[engine]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8722
diff changeset
   636
	return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   639
/** Move all free vehicles in the depot to the train */
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   640
static void NormalizeTrainVehInDepot(const Vehicle* u)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   642
	const Vehicle* v;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   643
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
	FOR_ALL_VEHICLES(v) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   645
		if (v->type == VEH_TRAIN && IsFreeWagon(v) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
				v->tile == u->tile &&
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
   647
				v->u.rail.track == TRACK_BIT_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
   648
			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
   649
					CMD_MOVE_RAIL_VEHICLE)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
   655
static CommandCost EstimateTrainCost(EngineID engine, const RailVehicleInfo* rvi)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
{
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8722
diff changeset
   657
	return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine, 0x17, rvi->base_cost) * (_price.build_railvehicle >> 3) >> 5);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2805
diff changeset
   660
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
   661
{
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   662
	u = new (u) Train();
1060
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   663
	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
   664
	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
   665
	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
   666
	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
   667
	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
   668
	u->z_pos = v->z_pos;
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
   669
	u->u.rail.track = TRACK_BIT_DEPOT;
1060
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   670
	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
   671
	u->subtype = 0;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   672
	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
   673
	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
   674
	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
   675
	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
   676
	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
   677
	u->u.rail.railtype = v->u.rail.railtype;
7989
116c8f9769af (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7988
diff changeset
   678
	if (building) v->SetNext(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
   679
	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
   680
	u->build_year = v->build_year;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   681
	if (building) v->value >>= 1;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   682
	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
   683
	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
   684
	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
   685
	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
   686
}
e8c44e426175 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   687
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   688
/** 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
   689
 * @param tile tile of the depot where rail-vehicle is built
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   690
 * @param flags type of operation
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   691
 * @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
   692
 * @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
   693
 *           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
   694
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
   695
CommandCost 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
   696
{
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   697
	/* Check if the engine-type is valid (for the player) */
6903
0b3d43bd1fdb (svn r9543) -Fix [FS#691]: Split up STR_ENGINE_NOT_BUILDABLE into four strings so it can be
maedhros
parents: 6683
diff changeset
   698
	if (!IsEngineBuildable(p1, VEH_TRAIN, _current_player)) return_cmd_error(STR_RAIL_VEHICLE_NOT_AVAILABLE);
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
   699
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   700
	/* 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
   701
	 * 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
   702
	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
   703
		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
   704
		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
   705
	}
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
   706
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   707
	const RailVehicleInfo *rvi = RailVehInfo(p1);
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   708
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
   709
	if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(p1, tile, flags);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
   711
	CommandCost value = EstimateTrainCost(p1, rvi);
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   712
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   713
	uint num_vehicles =
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   714
		(rvi->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) +
8091
f24d360b6ba1 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 8089
diff changeset
   715
		CountArticulatedParts(p1, false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
	if (!(flags & DC_QUERY_COST)) {
9277
c57fc7171a46 (svn r12482) -Fix: [build train] we don't need to have a depot in order to just check the price of a rail vehicle so don't check for compatible rails on the tile either
bjarni
parents: 9259
diff changeset
   718
		/* Check if depot and new engine uses the same kind of tracks *
c57fc7171a46 (svn r12482) -Fix: [build train] we don't need to have a depot in order to just check the price of a rail vehicle so don't check for compatible rails on the tile either
bjarni
parents: 9259
diff changeset
   719
		 * We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */
c57fc7171a46 (svn r12482) -Fix: [build train] we don't need to have a depot in order to just check the price of a rail vehicle so don't check for compatible rails on the tile either
bjarni
parents: 9259
diff changeset
   720
		if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
c57fc7171a46 (svn r12482) -Fix: [build train] we don't need to have a depot in order to just check the price of a rail vehicle so don't check for compatible rails on the tile either
bjarni
parents: 9259
diff changeset
   721
8091
f24d360b6ba1 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 8089
diff changeset
   722
		/* Allow for the dual-heads and the articulated parts, plus one to "terminate" the list. */
f24d360b6ba1 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 8089
diff changeset
   723
		Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
f24d360b6ba1 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 8089
diff changeset
   724
		memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
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
   725
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7867
diff changeset
   726
		if (!Vehicle::AllocateList(vl, num_vehicles))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
			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
   728
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   729
		Vehicle *v = vl[0];
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   730
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   731
		UnitID 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
   732
		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
   733
			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
   734
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
		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
   736
			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
   737
			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
   738
			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
   739
8279
9d4d45cfe301 (svn r11333) -Fix: Train engines still had a value of 0.
maedhros
parents: 8278
diff changeset
   740
			v = new (v) Train();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
			v->unitnumber = unit_num;
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   742
			v->direction = DiagDirToDir(dir);
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   743
			v->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   744
			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
   745
			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
   746
			v->y_pos = y;
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   747
			v->z_pos = GetSlopeZ(x, y);
9052
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
   748
			v->running_ticks = 0;
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
   749
			v->u.rail.track = TRACK_BIT_DEPOT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   750
			v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   751
			v->spritenum = rvi->image_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
			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
   753
			v->cargo_subtype = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
			v->cargo_cap = rvi->capacity;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
			v->max_speed = rvi->max_speed;
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   756
			v->value = value.GetCost();
1266
9dc5638fe8cc (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   757
			v->last_station_visited = INVALID_STATION;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
			v->dest_tile = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   759
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
   760
			v->engine_type = p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   761
6074
e70d63ef4d62 (svn r8385) -Fix
tron
parents: 6020
diff changeset
   762
			const Engine *e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
			v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
			v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   765
			v->max_age = e->lifelength * 366;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   766
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8753
diff changeset
   767
			v->name = NULL;
6074
e70d63ef4d62 (svn r8385) -Fix
tron
parents: 6020
diff changeset
   768
			v->u.rail.railtype = rvi->railtype;
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   769
			_new_vehicle_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   770
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   771
			v->service_interval = _patches.servint_trains;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   772
			v->date_of_last_service = _date;
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   773
			v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   774
			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
   775
			v->random_bits = VehicleRandomBits();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   776
6502
7ca154aca2b7 (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6501
diff changeset
   777
			v->vehicle_flags = 0;
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
   778
			if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
6502
7ca154aca2b7 (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6501
diff changeset
   779
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
   780
			v->group_id = DEFAULT_GROUP;
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
   781
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   782
			v->subtype = 0;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   783
			SetFrontEngine(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   784
			SetTrainEngine(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   785
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
			VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
   788
			if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   789
				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
   790
				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
   791
				/* 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
   792
				 * 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
   793
				 * 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
   794
				 */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   795
				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
   796
				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
   797
			} else {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7285
diff changeset
   798
				AddArticulatedParts(vl, VEH_TRAIN);
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   799
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
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
   801
			TrainConsistChanged(v);
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
   802
			UpdateTrainGroupID(v);
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   803
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
   804
			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
   805
				NormalizeTrainVehInDepot(v);
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   806
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
   808
			InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   809
			RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   810
			InvalidateWindow(WC_COMPANY, v->owner);
6195
b90cf92697b9 (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 6173
diff changeset
   811
			if (IsLocalPlayer())
7921
f99d5c351e75 (svn r10823) -Fix r8610 [FS#1097]: the autoreplace window vehicle count didn't always update correctly) (Matthias)
bjarni
parents: 7894
diff changeset
   812
				InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
6195
b90cf92697b9 (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 6173
diff changeset
   813
b90cf92697b9 (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 6173
diff changeset
   814
			GetPlayer(_current_player)->num_engines[p1]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   815
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
	}
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
   817
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   818
	return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   822
/* 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
   823
 * 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
   824
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
   825
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
	TileIndex tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   827
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
	/* 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
   829
	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
   830
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   831
	int count = 0;
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
   832
	for (; v != NULL; v = v->Next()) {
2844
7a55ad0a6a36 (svn r3392) Train depot fixes:
peter1138
parents: 2840
diff changeset
   833
		/* 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
   834
		 * 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
   835
		 * 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
   836
		 *
87cf4955787d (svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents: 3977
diff changeset
   837
		 * Also skip counting rear ends of multiheaded engines */
8022
32b885de2410 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni
parents: 7998
diff changeset
   838
		if (!IsArticulatedPart(v) && !IsRearDualheaded(v)) count++;
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
   839
		if (v->u.rail.track != TRACK_BIT_DEPOT || 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
   840
				(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
   841
			return -1;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   842
		}
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   843
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   844
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
	return count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   846
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   848
/* Used to check if the train is inside the depot and verifying that the VS_STOPPED flag is set */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   849
int CheckTrainStoppedInDepot(const Vehicle *v)
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   850
{
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   851
	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
   852
}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   853
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   854
/* 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
   855
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
   856
{
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   857
	return CheckTrainInDepot(v, false) > 0;
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   858
}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   859
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   860
/**
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   861
 * 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
   862
 * @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
   863
 * @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
   864
 * @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
   865
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
{
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   868
	/* unlinking the first vehicle of the chain? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
	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
   870
		v = GetNextVehicle(v);
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   871
		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
   872
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   873
		if (IsTrainWagon(v)) SetFreeWagon(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   874
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
		return v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
	}
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
   877
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   878
	Vehicle *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
   879
	for (u = first; GetNextVehicle(u) != v; u = GetNextVehicle(u)) {}
7989
116c8f9769af (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7988
diff changeset
   880
	GetLastEnginePart(u)->SetNext(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
   881
	return first;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   882
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   884
static Vehicle *FindGoodVehiclePos(const Vehicle *src)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   885
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
	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
   887
	EngineID eng = src->engine_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   888
	TileIndex tile = src->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
	FOR_ALL_VEHICLES(dst) {
8192
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
   891
		if (dst->type == VEH_TRAIN && IsFreeWagon(dst) && dst->tile == tile && !HASBITS(dst->vehstatus, VS_CRASHED)) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   892
			/* check so all vehicles in the line have the same engine. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
			Vehicle *v = dst;
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   894
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
			while (v->engine_type == eng) {
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
   896
				v = v->Next();
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   897
				if (v == NULL) return dst;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   899
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   902
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   904
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   905
/*
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   906
 * 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
   907
 * 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
   908
 */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   909
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
   910
{
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   911
	UnlinkWagon(v, v->First());
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   912
	if (dest == NULL) return;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   913
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   914
	Vehicle *next = dest->Next();
8503
b09fd1daea07 (svn r11567) -Fix [FS#1512] (r11011): break the chain before moving a vehicle after another in the same chain
glx
parents: 8463
diff changeset
   915
	v->SetNext(NULL);
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
   916
	dest->SetNext(v);
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   917
	v->SetNext(next);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   918
	ClearFreeWagon(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   919
	ClearFrontEngine(v);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   920
}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   921
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   922
/*
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   923
 * 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
   924
 * 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
   925
 */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   926
static void NormaliseTrainConsist(Vehicle *v)
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   927
{
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   928
	if (IsFreeWagon(v)) return;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   929
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   930
	assert(IsFrontEngine(v));
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   931
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   932
	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
   933
		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
   934
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   935
		/* make sure that there are no free cars before next engine */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   936
		Vehicle *u;
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
   937
		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
   938
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   939
		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
   940
		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
   941
	}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   942
}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   943
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   944
/** 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
   945
 * @param tile unused
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   946
 * @param flags type of operation
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   947
 * @param p1 various bitstuffed elements
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   948
 * - p1 (bit  0 - 15) source vehicle index
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   949
 * - 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
   950
 * @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
   951
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
   952
CommandCost 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
   953
{
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
   954
	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
   955
	VehicleID d = GB(p1, 16, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   957
	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
   958
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   959
	Vehicle *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
   960
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   961
	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
   962
8192
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
   963
	/* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
   964
	if (HASBITS(src->vehstatus, VS_CRASHED)) return CMD_ERROR;
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
   965
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   966
	/* if nothing is selected as destination, try and find a matching vehicle to drag to. */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   967
	Vehicle *dst;
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
   968
	if (d == INVALID_VEHICLE) {
4198
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
   969
		dst = IsTrainEngine(src) ? NULL : FindGoodVehiclePos(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
	} else {
6462
f57392bee868 (svn r8878) -Fix
tron
parents: 6440
diff changeset
   971
		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
   972
		dst = GetVehicle(d);
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   973
		if (dst->type != VEH_TRAIN || !CheckOwnership(dst->owner)) return CMD_ERROR;
8192
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
   974
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
   975
		/* Do not allow appending to crashed vehicles, too */
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
   976
		if (HASBITS(dst->vehstatus, VS_CRASHED)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   979
	/* if an articulated part is being handled, deal with its parent vehicle */
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   980
	while (IsArticulatedPart(src)) src = src->Previous();
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   981
	if (dst != NULL) {
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   982
		while (IsArticulatedPart(dst)) dst = dst->Previous();
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   983
	}
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   984
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   985
	/* don't move the same vehicle.. */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   986
	if (src == dst) return CommandCost();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   987
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
	/* locate the head of the two chains */
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   989
	Vehicle *src_head = src->First();
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
   990
	Vehicle *dst_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
   991
	if (dst != NULL) {
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
   992
		dst_head = dst->First();
6462
f57392bee868 (svn r8878) -Fix
tron
parents: 6440
diff changeset
   993
		if (dst_head->tile != src_head->tile) return CMD_ERROR;
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
   994
		/* Now deal with articulated part of destination wagon */
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   995
		dst = GetLastEnginePart(dst);
6462
f57392bee868 (svn r8878) -Fix
tron
parents: 6440
diff changeset
   996
	} else {
f57392bee868 (svn r8878) -Fix
tron
parents: 6440
diff changeset
   997
		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
   998
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   999
8022
32b885de2410 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni
parents: 7998
diff changeset
  1000
	if (IsRearDualheaded(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1001
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1002
	/* when moving all wagons, we can't have the same src_head and dst_head */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1003
	if (HasBit(p2, 0) && src_head == dst_head) return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1005
	{
9049
7e716ad214c6 (svn r12131) -Fix (r3374): with mammoth trains disabled, maximum train length was limited to 9
smatz
parents: 9047
diff changeset
  1006
		int max_len = _patches.mammoth_trains ? 100 : 10;
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1007
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1008
		/* check if all vehicles in the source train are stopped inside a depot. */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1009
		int 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
  1010
		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
  1011
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1012
		/* check the destination row if the source and destination aren't the same. */
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1013
		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
  1014
			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
  1015
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1016
			if (dst_head != NULL) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1017
				/* check if all vehicles in the dest train are stopped. */
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1018
				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
  1019
				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
  1020
			}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1021
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1022
			/* We are moving between rows, so only count the wagons from the source
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1023
			 * row that are being moved. */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1024
			if (HasBit(p2, 0)) {
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1025
				const Vehicle *u;
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1026
				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
  1027
					src_len--;
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1028
			} else {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1029
				/* If moving only one vehicle, just count that. */
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1030
				src_len = 1;
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1031
			}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1032
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1033
			if (src_len + dst_len > max_len) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1034
				/* Abort if we're adding too many wagons to a train. */
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1035
				if (dst_head != NULL && IsFrontEngine(dst_head)) return_cmd_error(STR_8819_TRAIN_TOO_LONG);
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1036
				/* Abort if we're making a train on a new row. */
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1037
				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
  1038
			}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1039
		} else {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1040
			/* Abort if we're creating a new train on an existing row. */
2883
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1041
			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
  1042
				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
  1043
		}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1044
	}
e1efe385e89c (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1045
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1046
	/* 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
  1047
	if (dst == NULL && !IsFrontEngine(src) && IsTrainEngine(src)) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  1048
		UnitID unit_num = GetFreeUnitNumber(VEH_TRAIN);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
		if (unit_num > _patches.max_trains)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
			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
  1051
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
  1052
		if (flags & DC_EXEC) src->unitnumber = unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
8658
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1055
	/*
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1056
	 * Check whether the vehicles in the source chain are in the destination
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1057
	 * chain. This can easily be done by checking whether the first vehicle
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1058
	 * of the source chain is in the destination chain as the Next/Previous
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1059
	 * pointers always make a doubly linked list of it where the assumption
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1060
	 * v->Next()->Previous() == v holds (assuming v->Next() != NULL).
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1061
	 */
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1062
	bool src_in_dst = false;
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1063
	for (Vehicle *v = dst_head; !src_in_dst && v != NULL; v = v->Next()) src_in_dst = v == src;
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1064
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1065
	/*
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1066
	 * If the source chain is in the destination chain then the user is
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1067
	 * only reordering the vehicles, thus not attaching a new vehicle.
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1068
	 * Therefor the 'allow wagon attach' callback does not need to be
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1069
	 * called. If it would be called strange things would happen because
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1070
	 * one 'attaches' an already 'attached' vehicle causing more trouble
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1071
	 * than it actually solves (infinite loops and such).
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1072
	 */
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1073
	if (dst_head != NULL && !src_in_dst) {
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1074
		/*
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1075
		 * When performing the 'allow wagon attach' callback, we have to check
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1076
		 * that for each and every wagon, not only the first one. This means
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1077
		 * that we have to test one wagon, attach it to the train and then test
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1078
		 * the next wagon till we have reached the end. We have to restore it
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1079
		 * to the state it was before we 'tried' attaching the train when the
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1080
		 * attaching fails or succeeds because we are not 'only' doing this
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1081
		 * in the DC_EXEC state.
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1082
		 */
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1083
		Vehicle *dst_tail = dst_head;
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1084
		while (dst_tail->Next() != NULL) dst_tail = dst_tail->Next();
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1085
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1086
		Vehicle *orig_tail = dst_tail;
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1087
		Vehicle *next_to_attach = src;
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1088
		Vehicle *src_previous = src->Previous();
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1089
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1090
		while (next_to_attach != NULL) {
8664
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1091
			/* Back up and clear the first_engine data to avoid using wagon override group */
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1092
			EngineID first_engine = next_to_attach->u.rail.first_engine;
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1093
			next_to_attach->u.rail.first_engine = INVALID_ENGINE;
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1094
8658
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1095
			uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, dst_head->engine_type, next_to_attach, dst_head);
8664
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1096
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1097
			/* Restore original first_engine data */
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1098
			next_to_attach->u.rail.first_engine = first_engine;
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1099
8658
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1100
			if (callback != CALLBACK_FAILED) {
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1101
				StringID error = STR_NULL;
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1102
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1103
				if (callback == 0xFD) error = STR_INCOMPATIBLE_RAIL_TYPES;
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1104
				if (callback < 0xFD) error = GetGRFStringID(GetEngineGRFID(dst_head->engine_type), 0xD000 + callback);
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1105
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1106
				if (error != STR_NULL) {
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1107
					/*
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1108
					 * The attaching is not allowed. In this case 'next_to_attach'
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1109
					 * can contain some vehicles of the 'source' and the destination
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1110
					 * train can have some too. We 'just' add the to-be added wagons
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1111
					 * to the chain and then split it where it was previously
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1112
					 * separated, i.e. the tail of the original destination train.
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1113
					 * Furthermore the 'previous' link of the original source vehicle needs
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1114
					 * to be restored, otherwise the train goes missing in the depot.
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1115
					 */
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1116
					dst_tail->SetNext(next_to_attach);
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1117
					orig_tail->SetNext(NULL);
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1118
					if (src_previous != NULL) src_previous->SetNext(src);
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1119
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1120
					return_cmd_error(error);
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1121
				}
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
  1122
			}
8658
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1123
8664
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1124
			/* Only check further wagons if told to move the chain */
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1125
			if (!HasBit(p2, 0)) break;
f3444d620972 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8658
diff changeset
  1126
8658
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1127
			/*
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1128
			 * Adding a next wagon to the chain so we can test the other wagons.
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1129
			 * First 'take' the first wagon from 'next_to_attach' and move it
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1130
			 * to the next wagon. Then add that to the tail of the destination
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1131
			 * train and update the tail with the new vehicle.
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1132
			 */
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1133
			Vehicle *to_add = next_to_attach;
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1134
			next_to_attach = next_to_attach->Next();
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1135
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1136
			to_add->SetNext(NULL);
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1137
			dst_tail->SetNext(to_add);
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1138
			dst_tail = dst_tail->Next();
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
  1139
		}
8658
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1140
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1141
		/*
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1142
		 * When we reach this the attaching is allowed. It also means that the
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1143
		 * chain of vehicles to attach is empty, so we do not need to merge that.
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1144
		 * This means only the splitting needs to be done.
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1145
		 * Furthermore the 'previous' link of the original source vehicle needs
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1146
		 * to be restored, otherwise the train goes missing in the depot.
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1147
		 */
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1148
		orig_tail->SetNext(NULL);
fcfacbbf4400 (svn r11724) -Fix: make it impossible (for users) to circumvent the length checking of the NewGRF 'allow wagon attach' callback by moving several wagons at a time.
rubidium
parents: 8653
diff changeset
  1149
		if (src_previous != NULL) src_previous->SetNext(src);
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
  1150
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
	/* do it? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
	if (flags & DC_EXEC) {
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1154
		/* If we move the front Engine and if the second vehicle is not an engine
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1155
		   add the whole vehicle to the DEFAULT_GROUP */
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1156
		if (IsFrontEngine(src) && !IsDefaultGroupID(src->group_id)) {
8025
39c5dc3c2b4a (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 8022
diff changeset
  1157
			Vehicle *v = GetNextVehicle(src);
39c5dc3c2b4a (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 8022
diff changeset
  1158
39c5dc3c2b4a (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 8022
diff changeset
  1159
			if (v != NULL && IsTrainEngine(v)) {
39c5dc3c2b4a (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 8022
diff changeset
  1160
				v->group_id   = src->group_id;
39c5dc3c2b4a (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 8022
diff changeset
  1161
				src->group_id = DEFAULT_GROUP;
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1162
			}
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1163
		}
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1164
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1165
		if (HasBit(p2, 0)) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1166
			/* unlink ALL wagons */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1167
			if (src != src_head) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1168
				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
  1169
				while (GetNextVehicle(v) != src) v = GetNextVehicle(v);
7989
116c8f9769af (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7988
diff changeset
  1170
				GetLastEnginePart(v)->SetNext(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
  1171
			} else {
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1172
				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
  1173
				src_head = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1174
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
		} else {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1176
			/* 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
  1177
			if (src_head == dst_head) dst_head = NULL;
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1178
			/* 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
  1179
			src_head = UnlinkWagon(src, src_head);
7989
116c8f9769af (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7988
diff changeset
  1180
			GetLastEnginePart(src)->SetNext(NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1182
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1183
		if (dst == NULL) {
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1184
			/* 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
  1185
			InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1186
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1187
			/* move the train to an empty line. for locomotives, we set the type to TS_Front. for wagons, 4. */
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1188
			if (IsTrainEngine(src)) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1189
				if (!IsFrontEngine(src)) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1190
					/* 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
  1191
					SetFrontEngine(src);
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1192
					assert(src->orders == NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1193
					src->num_orders = 0;
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1194
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1195
					// Decrease the engines number of the src engine_type
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1196
					if (!IsDefaultGroupID(src->group_id) && IsValidGroupID(src->group_id)) {
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1197
						GetGroup(src->group_id)->num_engines[src->engine_type]--;
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1198
					}
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1199
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1200
					// If we move an engine to a new line affect it to the DEFAULT_GROUP
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1201
					src->group_id = DEFAULT_GROUP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1202
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1203
			} else {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1204
				SetFreeWagon(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1205
			}
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
  1206
			dst_head = src;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1207
		} else {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1208
			if (IsFrontEngine(src)) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1209
				/* 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
  1210
				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
  1211
				DeleteVehicleOrders(src);
8025
39c5dc3c2b4a (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 8022
diff changeset
  1212
				RemoveVehicleFromGroup(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1213
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1214
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1215
			if (IsFrontEngine(src) || IsFreeWagon(src)) {
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1216
				InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1217
				ClearFrontEngine(src);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1218
				ClearFreeWagon(src);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1219
				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
  1220
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1221
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1222
			/* link in the wagon(s) in the chain. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1223
			{
1472
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1224
				Vehicle *v;
043304846688 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1225
9191
ae14770c829a (svn r12368) -Codechange: use explicit body for loops and conditions and remove -Wno-empty-body from the configure script
smatz
parents: 9163
diff changeset
  1226
				for (v = src; GetNextVehicle(v) != NULL; v = GetNextVehicle(v)) {}
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  1227
				GetLastEnginePart(v)->SetNext(dst->Next());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1228
			}
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  1229
			dst->SetNext(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1230
		}
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
  1231
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1232
		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
  1233
			if (src->u.rail.other_multiheaded_part == src_head) {
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  1234
				src_head = src_head->Next();
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1235
			}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1236
			AddWagonToConsist(src->u.rail.other_multiheaded_part, src);
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1237
		}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1238
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
  1239
		/* 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
  1240
		 * 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
  1241
		 * 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
  1242
		if (src_head != NULL && !IsFrontEngine(src_head) && IsTrainEngine(src_head)) {
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1243
			/* As in CmdMoveRailVehicle src_head->group_id will be equal to DEFAULT_GROUP
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1244
			 * we need to save the group and reaffect it to src_head */
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1245
			const GroupID tmp_g = src_head->group_id;
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
  1246
			CmdMoveRailVehicle(0, flags, src_head->index | (INVALID_VEHICLE << 16), 1);
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1247
			SetTrainGroupID(src_head, tmp_g);
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
  1248
			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
  1249
		}
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
  1250
4198
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1251
		if (src_head != NULL) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1252
			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
  1253
			TrainConsistChanged(src_head);
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1254
			UpdateTrainGroupID(src_head);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1255
			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
  1256
				/* 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
  1257
				InvalidateWindow(WC_VEHICLE_REFIT, src_head->index);
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  1258
				InvalidateWindowWidget(WC_VEHICLE_VIEW, src_head->index, VVW_WIDGET_REFIT_VEH);
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
			}
74cab7e464d1 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1260
			/* 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
  1261
			InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile);
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1262
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
4198
fcb0808731db (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1264
		if (dst_head != NULL) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1265
			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
  1266
			TrainConsistChanged(dst_head);
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1267
			UpdateTrainGroupID(dst_head);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1268
			if (IsFrontEngine(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
  1269
				/* Update the refit button and window */
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  1270
				InvalidateWindowWidget(WC_VEHICLE_VIEW, dst_head->index, VVW_WIDGET_REFIT_VEH);
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
  1271
				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
  1272
			}
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
  1273
			/* 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
  1274
			InvalidateWindow(WC_VEHICLE_DEPOT, dst_head->tile);
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
737
ceb3dce50a37 (svn r1189) Fix vehicle list update glitch when moving waggons in depots
tron
parents: 715
diff changeset
  1277
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1278
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1279
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1280
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1281
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1282
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1283
/** 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
  1284
 * @param tile unused
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1285
 * @param flags type of operation
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1286
 * @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
  1287
 * @param p2 unused
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1288
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
  1289
CommandCost 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
  1290
{
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1291
	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
  1292
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1293
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1294
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  1295
	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
  1296
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
  1297
	/* 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
  1298
	 * return 0xFF if it can. */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1299
	uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, 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
  1300
	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
  1301
		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
  1302
		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
  1303
	}
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
  1304
4251
85befcd5f435 (svn r5853) -Fix: [elrails] FS#178 Electric Trains can leave Conventional Depot
bjarni
parents: 4244
diff changeset
  1305
	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
  1306
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1307
	if (flags & DC_EXEC) {
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  1308
		if (v->vehstatus & VS_STOPPED && v->u.rail.track == TRACK_BIT_DEPOT) {
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3022
diff changeset
  1309
			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
  1310
		}
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3022
diff changeset
  1311
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1312
		v->vehstatus ^= VS_STOPPED;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  1313
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1314
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1315
	}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1316
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1317
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1318
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1319
/** 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
  1320
 * @param tile unused
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1321
 * @param flags type of operation
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
  1322
 * @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
  1323
 * @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
  1324
 * - 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
  1325
 * - 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
  1326
             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
  1327
 * - 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
  1328
 *           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
  1329
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
  1330
CommandCost 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
  1331
{
7285
d7cf450a59b9 (svn r10028) -Fix (r2270/r2951): When deleting the first engine of a train with multiple engines, only reopen the train window if the player had the original train window open. This fixes 'random' windows opening for multiple players of the same company.
peter1138
parents: 7284
diff changeset
  1332
	/* Check if we deleted a vehicle window */
d7cf450a59b9 (svn r10028) -Fix (r2270/r2951): When deleting the first engine of a train with multiple engines, only reopen the train window if the player had the original train window open. This fixes 'random' windows opening for multiple players of the same company.
peter1138
parents: 7284
diff changeset
  1333
	Window *w = NULL;
d7cf450a59b9 (svn r10028) -Fix (r2270/r2951): When deleting the first engine of a train with multiple engines, only reopen the train window if the player had the original train window open. This fixes 'random' windows opening for multiple players of the same company.
peter1138
parents: 7284
diff changeset
  1334
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1335
	if (!IsValidVehicleID(p1) || p2 > 2) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1336
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1337
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1338
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  1339
	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
  1340
8191
3ffc17ef4ff5 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 8138
diff changeset
  1341
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
3ffc17ef4ff5 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 8138
diff changeset
  1342
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
  1343
	while (IsArticulatedPart(v)) v = v->Previous();
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
  1344
	Vehicle *first = v->First();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1345
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1346
	/* 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
  1347
	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
  1348
		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
  1349
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1350
8022
32b885de2410 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni
parents: 7998
diff changeset
  1351
	if (IsRearDualheaded(v)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1352
1842
7efaede8ac8e (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1353
	if (flags & DC_EXEC) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1354
		if (v == first && IsFrontEngine(first)) {
7285
d7cf450a59b9 (svn r10028) -Fix (r2270/r2951): When deleting the first engine of a train with multiple engines, only reopen the train window if the player had the original train window open. This fixes 'random' windows opening for multiple players of the same company.
peter1138
parents: 7284
diff changeset
  1355
			w = FindWindowById(WC_VEHICLE_VIEW, first->index);
d7cf450a59b9 (svn r10028) -Fix (r2270/r2951): When deleting the first engine of a train with multiple engines, only reopen the train window if the player had the original train window open. This fixes 'random' windows opening for multiple players of the same company.
peter1138
parents: 7284
diff changeset
  1356
			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
  1357
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1358
		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
  1359
		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
  1360
	}
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
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8722
diff changeset
  1362
	CommandCost cost(EXPENSES_NEW_VEHICLES);
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
  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) {
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7476
diff changeset
  1374
				cost.AddCost(-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);
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7867
diff changeset
  1378
					delete 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) {
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1385
				Vehicle *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.2 If there are wagons present after the deleted front engine, check
6648
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1388
				 * if the second wagon (which will be first) is an engine. If it is one,
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1389
				 * promote it as a new train, retaining the unitnumber, orders */
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1390
				if (new_f != NULL && IsTrainEngine(new_f)) {
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1391
					switch_engine = true;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1392
					/* Copy important data from the front engine */
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1393
					new_f->unitnumber      = first->unitnumber;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1394
					new_f->current_order   = first->current_order;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1395
					new_f->cur_order_index = first->cur_order_index;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1396
					new_f->orders          = first->orders;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1397
					new_f->num_orders      = first->num_orders;
8025
39c5dc3c2b4a (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 8022
diff changeset
  1398
					new_f->group_id        = first->group_id;
6648
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1399
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1400
					if (first->prev_shared != NULL) {
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1401
						first->prev_shared->next_shared = new_f;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1402
						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
  1403
					}
6648
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1404
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1405
					if (first->next_shared != NULL) {
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1406
						first->next_shared->prev_shared = new_f;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1407
						new_f->next_shared = first->next_shared;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1408
					}
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1409
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1410
					/*
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1411
					 * Remove all order information from the front train, to
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1412
					 * prevent the order and the shared order list to be
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1413
					 * destroyed by Destroy/DeleteVehicle.
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1414
					 */
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1415
					first->orders      = NULL;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1416
					first->prev_shared = NULL;
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1417
					first->next_shared = NULL;
8025
39c5dc3c2b4a (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 8022
diff changeset
  1418
					first->group_id    = DEFAULT_GROUP;
6648
fe5626842ec1 (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6642
diff changeset
  1419
7285
d7cf450a59b9 (svn r10028) -Fix (r2270/r2951): When deleting the first engine of a train with multiple engines, only reopen the train window if the player had the original train window open. This fixes 'random' windows opening for multiple players of the same company.
peter1138
parents: 7284
diff changeset
  1420
					/* If we deleted a window then open a new one for the 'new' train */
7982
539e32cc37ce (svn r10997) -Codechange: unify the vehicle view window. Patch by PhilSophus.
rubidium
parents: 7973
diff changeset
  1421
					if (IsLocalPlayer() && w != NULL) ShowVehicleViewWindow(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
  1422
				}
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
			/* 3. Delete the requested wagon */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1426
			cost.AddCost(-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
  1427
			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
  1428
				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
  1429
				DeleteDepotHighlightOfVehicle(v);
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7867
diff changeset
  1430
				delete 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
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
				/* 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
  1433
					* 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
  1434
				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
  1435
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
				/* 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
  1437
				if (first != NULL) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1438
					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
  1439
					TrainConsistChanged(first);
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1440
					UpdateTrainGroupID(first);
9202
6c1679b46549 (svn r12380) -Fix: update train acceleration and max speed after setting cached value to ensure the correct max speed is used with disabled real acceleration
glx
parents: 9201
diff changeset
  1441
					if (IsFrontEngine(first)) InvalidateWindow(WC_VEHICLE_REFIT, first->index);
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
  1442
				}
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
  1443
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
  1444
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
  1445
				/* (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
  1446
				 * 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
  1447
				 * 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
  1448
				 * engines to its train anyways */
9108
82c3c4db5870 (svn r12194) -Codechange: apply coding style on enum TrainSubtype
smatz
parents: 9103
diff changeset
  1449
				if (p2 == 2 && HasBit(ori_subtype, TS_FRONT)) {
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1450
					Vehicle *tmp;
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
  1451
					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
  1452
						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
  1453
						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
  1454
					}
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
  1455
				}
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
  1456
			}
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
		} 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
  1458
		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
  1459
			/* 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
  1460
			 * 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
  1461
			 * earlier in the chain (before deletion), leave it alone */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1462
			Vehicle *tmp;
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
  1463
			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
  1464
				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
  1465
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1466
				if (IsMultiheaded(v)) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1467
					if (IsTrainEngine(v)) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1468
						/* 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
  1469
						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
  1470
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1471
						if (rear != NULL) {
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1472
							cost.AddCost(-rear->value);
7281
d8790704c8b0 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 7280
diff changeset
  1473
d8790704c8b0 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 7280
diff changeset
  1474
							/* If this is a multiheaded vehicle with nothing
d8790704c8b0 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 7280
diff changeset
  1475
							 * between the parts, tmp will be pointing to the
d8790704c8b0 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 7280
diff changeset
  1476
							 * rear part, which is unlinked from the train and
d8790704c8b0 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 7280
diff changeset
  1477
							 * deleted here. However, because tmp has already
d8790704c8b0 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 7280
diff changeset
  1478
							 * been set it needs to be updated now so that the
d8790704c8b0 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 7280
diff changeset
  1479
							 * loop never sees the rear part. */
d8790704c8b0 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 7280
diff changeset
  1480
							if (tmp == rear) tmp = GetNextVehicle(tmp);
d8790704c8b0 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 7280
diff changeset
  1481
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1482
							if (flags & DC_EXEC) {
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1483
								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
  1484
								DeleteDepotHighlightOfVehicle(rear);
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7867
diff changeset
  1485
								delete rear;
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1486
							}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1487
						}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1488
					} 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
  1489
						/* 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
  1490
						continue;
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1491
					}
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
  1492
				}
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
  1493
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1494
				cost.AddCost(-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
  1495
				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
  1496
					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
  1497
					DeleteDepotHighlightOfVehicle(v);
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7867
diff changeset
  1498
					delete 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
  1499
				}
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
			}
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
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
  1502
			/* 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
  1503
			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
  1504
				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
  1505
				TrainConsistChanged(first);
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  1506
				UpdateTrainGroupID(first);
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1507
				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
  1508
			}
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
  1509
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1510
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1511
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1512
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7050
diff changeset
  1514
void Train::UpdateDeltaXY(Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1515
{
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7050
diff changeset
  1516
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1518
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1519
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1520
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1521
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1523
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1525
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1527
#undef MKIT
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
	uint32 x = _delta_xy_table[direction];
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7050
diff changeset
  1530
	this->x_offs        = GB(x,  0, 8);
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7050
diff changeset
  1531
	this->y_offs        = GB(x,  8, 8);
9289
92f1249c304e (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 9282
diff changeset
  1532
	this->x_extent      = GB(x, 16, 8);
92f1249c304e (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 9282
diff changeset
  1533
	this->y_extent      = GB(x, 24, 8);
92f1249c304e (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 9282
diff changeset
  1534
	this->z_extent      = 6;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1537
static void UpdateVarsAfterSwap(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
{
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7050
diff changeset
  1539
	v->UpdateDeltaXY(v->direction);
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7605
diff changeset
  1540
	v->cur_image = v->GetImage(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1541
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1542
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1543
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1544
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1545
9103
ef4a10ed7f9f (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 9102
diff changeset
  1546
static inline void SetLastSpeed(Vehicle* v, int spd)
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1547
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1548
	int old = v->u.rail.last_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1549
	if (spd != old) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1550
		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
  1551
		if (_patches.vehicle_speed || (old == 0) != (spd == 0))
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  1552
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1553
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1554
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1555
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1556
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
  1557
{
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1558
	byte flag1 = *swap_flag1;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1559
	byte flag2 = *swap_flag2;
954
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
	/* Clear the flags */
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  1562
	ClrBit(*swap_flag1, VRF_GOINGUP);
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  1563
	ClrBit(*swap_flag1, VRF_GOINGDOWN);
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  1564
	ClrBit(*swap_flag2, VRF_GOINGUP);
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  1565
	ClrBit(*swap_flag2, VRF_GOINGDOWN);
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1566
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1567
	/* Reverse the rail-flags (if needed) */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1568
	if (HasBit(flag1, VRF_GOINGUP)) {
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
  1569
		SetBit(*swap_flag2, VRF_GOINGDOWN);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1570
	} else if (HasBit(flag1, VRF_GOINGDOWN)) {
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
  1571
		SetBit(*swap_flag2, VRF_GOINGUP);
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1572
	}
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1573
	if (HasBit(flag2, VRF_GOINGUP)) {
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
  1574
		SetBit(*swap_flag1, VRF_GOINGDOWN);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1575
	} else if (HasBit(flag2, VRF_GOINGDOWN)) {
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
  1576
		SetBit(*swap_flag1, VRF_GOINGUP);
954
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
}
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1579
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1580
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1581
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1582
	Vehicle *a, *b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1583
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1584
	/* locate vehicles to swap */
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  1585
	for (a = v; l != 0; l--) a = a->Next();
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  1586
	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
  1587
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1588
	if (a != b) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
		/* swap the hidden bits */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1590
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1591
			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
  1592
			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
  1593
			a->vehstatus = tmp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1594
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1595
5984
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5919
diff changeset
  1596
		Swap(a->u.rail.track, b->u.rail.track);
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5919
diff changeset
  1597
		Swap(a->direction,    b->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1598
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1599
		/* toggle direction */
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  1600
		if (a->u.rail.track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction);
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  1601
		if (b->u.rail.track != TRACK_BIT_DEPOT) b->direction = ReverseDir(b->direction);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1602
5984
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5919
diff changeset
  1603
		Swap(a->x_pos, b->x_pos);
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5919
diff changeset
  1604
		Swap(a->y_pos, b->y_pos);
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5919
diff changeset
  1605
		Swap(a->tile,  b->tile);
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5919
diff changeset
  1606
		Swap(a->z_pos, b->z_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1607
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1608
		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
  1609
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1610
		/* update other vars */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1611
		UpdateVarsAfterSwap(a);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1612
		UpdateVarsAfterSwap(b);
1554
c0b2287254f4 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1613
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
  1614
		/* call the proper EnterTile function unless we are in a wormhole */
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  1615
		if (a->u.rail.track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  1616
		if (b->u.rail.track != TRACK_BIT_WORMHOLE) 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
  1617
	} else {
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  1618
		if (a->u.rail.track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1619
		UpdateVarsAfterSwap(a);
1554
c0b2287254f4 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1620
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  1621
		if (a->u.rail.track != TRACK_BIT_WORMHOLE) 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
  1622
	}
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
  1623
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
  1624
	/* 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
  1625
	TrainPowerChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1626
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1627
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1628
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1629
/**
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1630
 * Check if the vehicle is a train
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1631
 * @param v vehicle on tile
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1632
 * @return v if it is a train, NULL otherwise
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1633
 */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1634
static void *TrainOnTileEnum(Vehicle *v, void *)
744
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1635
{
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1636
	return (v->type == VEH_TRAIN) ? v : NULL;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1637
}
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1638
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1639
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1640
/**
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1641
 * Checks if a train is approaching a rail-road crossing
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1642
 * @param v vehicle on tile
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1643
 * @param data tile with crossing we are testing
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1644
 * @return v if it is approaching a crossing, NULL otherwise
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1645
 */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1646
static void *TrainApproachingCrossingEnum(Vehicle *v, void *data)
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1647
{
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1648
	/* not a train || not front engine || crashed */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1649
	if (v->type != VEH_TRAIN || !IsFrontEngine(v) || v->vehstatus & VS_CRASHED) return NULL;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1650
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1651
	TileIndex tile = *(TileIndex*)data;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1652
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1653
	if (TrainApproachingCrossingTile(v) != tile) return NULL;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1654
744
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1655
	return v;
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1656
}
41293b73ebbb (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1657
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1658
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1659
/**
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1660
 * Finds a vehicle approaching rail-road crossing
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1661
 * @param tile tile to test
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1662
 * @return pointer to vehicle approaching the crossing
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1663
 * @pre tile is a rail-road crossing
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1664
 */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1665
static Vehicle *TrainApproachingCrossing(TileIndex tile)
1103
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1666
{
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1667
	assert(IsLevelCrossingTile(tile));
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1668
9094
58d1cddf1af6 (svn r12179) -Codechange: use GetCrossingRailTrack() and GetCrossingRailAxis() to improve code readability
smatz
parents: 9093
diff changeset
  1669
	DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile));
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1670
	TileIndex tile_from = tile + TileOffsByDiagDir(dir);
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1671
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1672
	Vehicle *v = (Vehicle *)VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum);
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1673
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1674
	if (v != NULL) return v;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1675
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1676
	dir = ReverseDiagDir(dir);
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1677
	tile_from = tile + TileOffsByDiagDir(dir);
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1678
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1679
	return (Vehicle *)VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum);
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1680
}
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1681
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1682
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1683
/**
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1684
 * Sets correct crossing state
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1685
 * @param tile tile to update
8840
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1686
 * @param sound should we play sound?
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1687
 * @pre tile is a rail-road crossing
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1688
 */
8840
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1689
void UpdateLevelCrossing(TileIndex tile, bool sound)
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1690
{
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1691
	assert(IsLevelCrossingTile(tile));
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1692
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1693
	/* train on crossing || train approaching crossing */
8840
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1694
	bool new_state = VehicleFromPos(tile, NULL, &TrainOnTileEnum) || TrainApproachingCrossing(tile);
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1695
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1696
	if (new_state != IsCrossingBarred(tile)) {
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1697
		if (new_state && sound) {
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1698
			SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1699
		}
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1700
		SetCrossingBarred(tile, new_state);
81cfba69eddd (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8838
diff changeset
  1701
		MarkTileDirtyByTile(tile);
1103
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1702
	}
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1703
}
d3b5e10c6a7e (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1704
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1705
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1706
/**
8852
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1707
 * Bars crossing and plays ding-ding sound if not barred already
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1708
 * @param tile tile with crossing
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1709
 * @pre tile is a rail-road crossing
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1710
 */
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1711
static inline void MaybeBarCrossingWithSound(TileIndex tile)
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1712
{
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1713
	if (!IsCrossingBarred(tile)) {
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1714
		BarCrossing(tile);
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1715
		SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1716
		MarkTileDirtyByTile(tile);
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1717
	}
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1718
}
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1719
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1720
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1721
/**
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1722
 * Advances wagons for train reversing, needed for variable length wagons.
9208
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1723
 * This one is called before the train is reversed.
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1724
 * @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
  1725
 */
9208
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1726
static void AdvanceWagonsBeforeSwap(Vehicle *v)
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1727
{
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1728
	Vehicle *base = v;
9208
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1729
	Vehicle *first = base;                    // first vehicle to move
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1730
	Vehicle *last = GetLastVehicleInChain(v); // last vehicle to move
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1731
	uint length = CountVehiclesInChain(v);
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1732
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1733
	while (length > 2) {
9208
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1734
		last = last->Previous();
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1735
		first = first->Next();
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1736
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1737
		int differential = base->u.rail.cached_veh_length - last->u.rail.cached_veh_length;
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1738
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1739
		/* do not update images now
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1740
		 * negative differential will be handled in AdvanceWagonsAfterSwap() */
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1741
		for (int i = 0; i < differential; i++) TrainController(first, last->Next(), false);
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1742
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1743
		base = first; // == base->Next()
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1744
		length -= 2;
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1745
	}
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1746
}
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1747
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1748
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1749
/**
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1750
 * Advances wagons for train reversing, needed for variable length wagons.
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1751
 * This one is called after the train is reversed.
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1752
 * @param v First vehicle in chain
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1753
 */
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1754
static void AdvanceWagonsAfterSwap(Vehicle *v)
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1755
{
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1756
	/* first of all, fix the situation when the train was entering a depot */
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1757
	Vehicle *dep = v; // last vehicle in front of just left depot
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1758
	while (dep->Next() != NULL && (dep->u.rail.track == TRACK_BIT_DEPOT || dep->Next()->u.rail.track != TRACK_BIT_DEPOT)) {
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1759
		dep = dep->Next(); // find first vehicle outside of a depot, with next vehicle inside a depot
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1760
	}
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1761
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1762
	Vehicle *leave = dep->Next(); // first vehicle in a depot we are leaving now
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1763
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1764
	if (leave != NULL) {
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1765
		/* 'pull' next wagon out of the depot, so we won't miss it (it could stay in depot forever) */
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1766
		int d = TicksToLeaveDepot(dep);
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1767
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1768
		if (d <= 0) {
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1769
			leave->vehstatus &= ~VS_HIDDEN; // move it out of the depot
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1770
			leave->u.rail.track = AxisToTrackBits(DiagDirToAxis(GetRailDepotDirection(leave->tile)));
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1771
			for (int i = 0; i >= d; i--) TrainController(leave, NULL, false); // maybe move it, and maybe let another wagon leave
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1772
		}
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1773
	} else {
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1774
		dep = NULL; // no vehicle in a depot, so no vehicle leaving a depot
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1775
	}
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1776
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1777
	Vehicle *base = v;
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1778
	Vehicle *first = base;                    // first vehicle to move
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1779
	Vehicle *last = GetLastVehicleInChain(v); // last vehicle to move
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1780
	uint length = CountVehiclesInChain(v);
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1781
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1782
	/* we have to make sure all wagons that leave a depot because of train reversing are moved coorectly
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1783
	 * they have already correct spacing, so we have to make sure they are moved how they should */
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1784
	bool nomove = (dep == NULL); // if there is no vehicle leaving a depot, limit the number of wagons moved immediatelly
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1785
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1786
	while (length > 2) {
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1787
		/* we reached vehicle (originally) in front of a depot, stop now
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1788
		 * (we would move wagons that are alredy moved with new wagon length) */
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1789
		if (base == dep) break;
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1790
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1791
		/* the last wagon was that one leaving a depot, so do not move it anymore */
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1792
		if (last == dep) nomove = true;
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1793
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1794
		last = last->Previous();
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1795
		first = first->Next();
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1796
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1797
		int differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length;
9208
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1798
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1799
		/* do not update images now */
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1800
		for (int i = 0; i < differential; i++) TrainController(first, (nomove ? last->Next() : NULL), false);
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1801
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1802
		base = first; // == base->Next()
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1803
		length -= 2;
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1804
	}
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1805
}
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1806
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2928
diff changeset
  1807
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1808
static void ReverseTrainDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1809
{
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1810
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) {
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1811
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1812
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1813
743
8161509f1864 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1814
	/* Check if we were approaching a rail/road-crossing */
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1815
	TileIndex crossing = TrainApproachingCrossingTile(v);
743
8161509f1864 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1816
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1817
	/* count number of vehicles */
8639
afebb18ecef5 (svn r11705) -Fix [FS#1557]: trains could have sprites with wrong direction when reversing, also was inconsistent with save/load process (possible desyncs)
smatz
parents: 8636
diff changeset
  1818
	int r = 0;  ///< number of vehicles - 1
afebb18ecef5 (svn r11705) -Fix [FS#1557]: trains could have sprites with wrong direction when reversing, also was inconsistent with save/load process (possible desyncs)
smatz
parents: 8636
diff changeset
  1819
	for (const Vehicle *u = v; (u = u->Next()) != NULL;) { r++; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1820
9208
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1821
	AdvanceWagonsBeforeSwap(v);
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1822
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1823
	/* swap start<>end, start+1<>end-1, ... */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1824
	int l = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1825
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1826
		ReverseTrainSwapVeh(v, l++, r--);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1827
	} while (l <= r);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1828
9208
f376b12acb1b (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 9206
diff changeset
  1829
	AdvanceWagonsAfterSwap(v);
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1830
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1831
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) {
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1832
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1833
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1834
9163
79101c0c3bc9 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 9162
diff changeset
  1835
	/* set reversed flag on all parts */
79101c0c3bc9 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 9162
diff changeset
  1836
	for (Vehicle *u = v; u != NULL; u = u->Next()) ToggleBit(u->u.rail.flags, VRF_TOGGLE_REVERSE);
8639
afebb18ecef5 (svn r11705) -Fix [FS#1557]: trains could have sprites with wrong direction when reversing, also was inconsistent with save/load process (possible desyncs)
smatz
parents: 8636
diff changeset
  1837
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  1838
	ClrBit(v->u.rail.flags, VRF_REVERSING);
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1839
9163
79101c0c3bc9 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 9162
diff changeset
  1840
	/* recalculate cached data */
79101c0c3bc9 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 9162
diff changeset
  1841
	TrainConsistChanged(v);
79101c0c3bc9 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 9162
diff changeset
  1842
79101c0c3bc9 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 9162
diff changeset
  1843
	/* update all images */
79101c0c3bc9 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 9162
diff changeset
  1844
	for (Vehicle *u = v; u != NULL; u = u->Next()) u->cur_image = u->GetImage(u->direction);
79101c0c3bc9 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 9162
diff changeset
  1845
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1846
	/* update crossing we were approaching */
8838
1549b7f9d0a8 (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8830
diff changeset
  1847
	if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1848
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1849
	/* maybe we are approaching crossing now, after reversal */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  1850
	crossing = TrainApproachingCrossingTile(v);
8852
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  1851
	if (crossing != INVALID_TILE) MaybeBarCrossingWithSound(crossing);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1852
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1853
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1854
/** 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
  1855
 * @param tile unused
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1856
 * @param flags type of operation
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1857
 * @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
  1858
 * @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
  1859
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
  1860
CommandCost 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
  1861
{
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1862
	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
  1863
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1864
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1865
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  1866
	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
  1867
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1868
	if (p2) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1869
		/* 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
  1870
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1871
		if (IsMultiheaded(v) || HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) {
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1872
			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
  1873
		}
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1874
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
  1875
		Vehicle *front = v->First();
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1876
		/* make sure the vehicle is stopped in the depot */
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1877
		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
  1878
			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
  1879
		}
3257
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1880
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1881
		if (flags & DC_EXEC) {
8428
f8300c908bd9 (svn r11485) -Codechange: Remove the doubled function ToggleBitT and rename the remaining to fit with the naming style
skidd13
parents: 8427
diff changeset
  1882
			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
  1883
			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
  1884
			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
  1885
		}
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1886
	} else {
7972
dff055fff851 (svn r10983) -Codechange: use vehstatus & VS_CRASHED instead of some other "methods" custom to each vehicle to determine whether the vehicle is crashed.
rubidium
parents: 7921
diff changeset
  1887
		/* turn the whole train around */
dff055fff851 (svn r10983) -Codechange: use vehstatus & VS_CRASHED instead of some other "methods" custom to each vehicle to determine whether the vehicle is crashed.
rubidium
parents: 7921
diff changeset
  1888
		if (v->vehstatus & VS_CRASHED || v->breakdown_ctr != 0) return CMD_ERROR;
3257
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1889
aed8705e3847 (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1890
		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
  1891
			if (_patches.realistic_acceleration && v->cur_speed != 0) {
8428
f8300c908bd9 (svn r11485) -Codechange: Remove the doubled function ToggleBitT and rename the remaining to fit with the naming style
skidd13
parents: 8427
diff changeset
  1892
				ToggleBit(v->u.rail.flags, VRF_REVERSING);
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1893
			} else {
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1894
				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
  1895
				SetLastSpeed(v, 0);
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1896
				ReverseTrainDirection(v);
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1897
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1898
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
	}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1900
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1901
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1902
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1903
/** 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
  1904
 * @param tile unused
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1905
 * @param flags type of operation
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1906
 * @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
  1907
 * @param p2 unused
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1908
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
  1909
CommandCost 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
  1910
{
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1911
	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
  1912
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1913
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1914
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  1915
	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
  1916
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1917
	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
  1918
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1919
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1920
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1921
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
  1922
/** 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
  1923
 * @param tile unused
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  1924
 * @param flags type of operation
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
  1925
 * @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
  1926
 * param p2 various bitstuffed elements
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1927
 * - 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
  1928
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
7042
d841fb0d8e9a (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6998
diff changeset
  1929
 * - p2 = (bit 16) - refit only this vehicle
d841fb0d8e9a (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6998
diff changeset
  1930
 * @return cost of refit or error
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
  1931
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
  1932
CommandCost 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
  1933
{
2635
88b8b74c01ac (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2634
diff changeset
  1934
	CargoID new_cid = GB(p2, 0, 8);
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1935
	byte new_subtype = GB(p2, 8, 8);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1936
	bool only_this = HasBit(p2, 16);
842
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1937
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1938
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1939
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1940
	Vehicle *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
  1941
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  1942
	if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR;
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
  1943
	if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
8641
966d79f41865 (svn r11707) -Fix: do not allow refitting flooded (destroyed) vehicles
smatz
parents: 8640
diff changeset
  1944
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
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
  1945
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
  1946
	/* Check cargo */
6642
2e690e6fdc03 (svn r9259) -Codechange: NUM_CARGO isn't a valid cargo type...
peter1138
parents: 6629
diff changeset
  1947
	if (new_cid >= NUM_CARGO) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1948
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8722
diff changeset
  1949
	CommandCost cost(EXPENSES_TRAIN_RUN);
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1950
	uint num = 0;
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1951
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1952
	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
  1953
		/* 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
  1954
		 * 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
  1955
		 * 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
  1956
		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
  1957
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
  1958
		if (v->cargo_cap != 0) {
1895
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1959
			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
  1960
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  1961
			if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_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
  1962
				/* 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
  1963
				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
  1964
				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
  1965
				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
  1966
				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
  1967
				/* 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
  1968
				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
  1969
				/* 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
  1970
				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
  1971
				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
  1972
			}
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1973
398f156479c8 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1974
			if (amount == CALLBACK_FAILED) { // callback failed or not used, use default
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1975
				const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1976
				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
  1977
				/* 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
  1978
				 * 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
  1979
				 * 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
  1980
				 */
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1981
				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
  1982
				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
  1983
					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
  1984
					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
  1985
					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
  1986
					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
  1987
				}
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
  1988
				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
  1989
					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
  1990
					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
  1991
					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
  1992
					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
  1993
				}
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  1994
			}
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1995
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1996
			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
  1997
				if (new_cid != v->cargo_type) {
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1998
					cost.AddCost(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
  1999
				}
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
  2000
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2001
				num += amount;
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2002
				if (flags & DC_EXEC) {
7506
e52d89f5c7c1 (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7498
diff changeset
  2003
					v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2004
					v->cargo_type = new_cid;
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2005
					v->cargo_cap = amount;
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  2006
					v->cargo_subtype = new_subtype;
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2007
					InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2008
					InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  2009
					RebuildVehicleLists();
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2010
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2011
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2012
		}
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  2013
	} while ((v = v->Next()) != NULL && !only_this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2014
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
  2015
	_returned_refit_capacity = num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2016
4708
5129e3add448 (svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents: 4656
diff changeset
  2017
	/* Update the train's cached variables */
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
  2018
	if (flags & DC_EXEC) TrainConsistChanged(GetVehicle(p1)->First());
4708
5129e3add448 (svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents: 4656
diff changeset
  2019
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2020
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2021
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2022
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2023
struct TrainFindDepotData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2024
	uint best_length;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  2025
	TileIndex tile;
2475
8443e1eefe1b (svn r3001) s/Player*/const Player*/
tron
parents: 2464
diff changeset
  2026
	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
  2027
	/**
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2028
	 * true if reversing is necessary for the train to get to this depot
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2029
	 * 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
  2030
	 */
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
  2031
	bool reverse;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2032
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2034
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
  2035
{
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2036
	if (IsTileType(tile, MP_RAILWAY) &&
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2037
			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
  2038
			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
  2039
		/* 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
  2040
		tfdd->best_length = length / DIAG_FACTOR;
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2041
		tfdd->tile = tile;
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2042
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2043
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2044
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2045
	return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2046
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2047
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2048
/** returns the tile of a depot to goto to. The given vehicle must not be
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2049
 * 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
  2050
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v, int max_distance)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2051
{
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2052
	assert(!(v->vehstatus & VS_CRASHED));
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2053
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2054
	TrainFindDepotData 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
  2055
	tfdd.owner = v->owner;
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2056
	tfdd.best_length = UINT_MAX;
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
  2057
	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
  2058
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2059
	TileIndex tile = v->tile;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2060
	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
  2061
		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
  2062
		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
  2063
		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
  2064
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2065
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2066
	switch (_patches.pathfinder_for_trains) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2067
		case VPF_YAPF: { /* YAPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2068
			bool found = YapfFindNearestRailDepotTwoWay(v, max_distance, NPF_INFINITE_PENALTY, &tfdd.tile, &tfdd.reverse);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2069
			tfdd.best_length = found ? max_distance / 2 : UINT_MAX; // some fake distance or NOT_FOUND
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2070
		} break;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2071
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2072
		case VPF_NPF: { /* NPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2073
			Vehicle* last = GetLastVehicleInChain(v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2074
			Trackdir trackdir = GetVehicleTrackdir(v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2075
			Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2076
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2077
			assert(trackdir != INVALID_TRACKDIR);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2078
			NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes, NPF_INFINITE_PENALTY);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2079
			if (ftd.best_bird_dist == 0) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2080
				/* Found target */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2081
				tfdd.tile = ftd.node.tile;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2082
				/* Our caller expects a number of tiles, so we just approximate that
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2083
				* number by this. It might not be completely what we want, but it will
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2084
				* work for now :-) We can possibly change this when the old pathfinder
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2085
				* is removed. */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2086
				tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2087
				if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) tfdd.reverse = true;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2088
			}
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2089
		} break;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2090
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2091
		default:
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2092
		case VPF_NTP: { /* NTP */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2093
			/* search in the forward direction first. */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2094
			DiagDirection i = TrainExitDir(v->direction, v->u.rail.track);
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
  2095
			NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2096
			if (tfdd.best_length == UINT_MAX){
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2097
				tfdd.reverse = true;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2098
				/* search in backwards direction */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2099
				i = TrainExitDir(ReverseDir(v->direction), v->u.rail.track);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2100
				NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2101
			}
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2102
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2103
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2104
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
  2105
	return tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2106
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2107
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  2108
/** 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
  2109
 * @param tile unused
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2110
 * @param flags type of operation
1784
6eb3ab1bc33c (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  2111
 * @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
  2112
 * @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
  2113
 * - 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
  2114
 * - 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
  2115
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7420
diff changeset
  2116
CommandCost 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
  2117
{
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2118
	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
  2119
		/* 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
  2120
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  2121
		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
  2122
	}
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  2123
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2124
	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
  2125
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2126
	Vehicle *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
  2127
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  2128
	if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR;
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2129
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2130
	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
  2131
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2132
	if (v->current_order.type == OT_GOTO_DEPOT) {
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  2133
		if (!!(p2 & DEPOT_SERVICE) == HasBit(v->current_order.flags, OF_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
  2134
			/* 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
  2135
			 * 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
  2136
			 * 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
  2137
			if (flags & DC_EXEC) {
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  2138
				ClrBit(v->current_order.flags, OF_PART_OF_ORDERS);
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  2139
				ToggleBit(v->current_order.flags, OF_HALT_IN_DEPOT);
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  2140
				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
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
  2141
			}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  2142
			return CommandCost();
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
  2143
		}
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
  2144
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2145
		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
  2146
		if (flags & DC_EXEC) {
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  2147
			if (HasBit(v->current_order.flags, OF_PART_OF_ORDERS)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2148
				v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2149
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2150
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2151
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2152
			v->current_order.flags = 0;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  2153
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2154
		}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  2155
		return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2156
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2157
4526
77ef74d28e76 (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
  2158
	/* 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
  2159
	 * 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
  2160
	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
  2161
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2162
	TrainFindDepotData 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
  2163
	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
  2164
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2165
	if (flags & DC_EXEC) {
6998
403314de0154 (svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents: 6987
diff changeset
  2166
		if (v->current_order.type == OT_LOADING) v->LeaveStation();
403314de0154 (svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents: 6987
diff changeset
  2167
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
  2168
		v->dest_tile = tfdd.tile;
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2169
		v->current_order.type = OT_GOTO_DEPOT;
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  2170
		v->current_order.flags = OFB_NON_STOP;
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  2171
		if (!(p2 & DEPOT_SERVICE)) SetBit(v->current_order.flags, OF_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
  2172
		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
  2173
		v->current_order.refit_cargo = CT_INVALID;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  2174
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
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
  2175
		/* If there is no depot in front, reverse automatically */
6998
403314de0154 (svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents: 6987
diff changeset
  2176
		if (tfdd.reverse) 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
  2177
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2178
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  2179
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2180
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2181
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2182
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6502
diff changeset
  2183
void OnTick_Train()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2184
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2185
	_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
  2186
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2187
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2188
static const int8 _vehicle_smoke_pos[8] = {
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2189
	1, 1, 1, 0, -1, -1, -1, 0
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2190
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2191
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  2192
static void HandleLocomotiveSmokeCloud(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2193
{
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2194
	bool sound = false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2195
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2196
	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
  2197
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2198
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2199
	const Vehicle* u = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2200
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2201
	do {
6074
e70d63ef4d62 (svn r8385) -Fix
tron
parents: 6020
diff changeset
  2202
		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2203
		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
  2204
		byte effect_type = GB(v->u.rail.cached_vis_effect, 4, 2);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  2205
		bool disable_effect = HasBit(v->u.rail.cached_vis_effect, 6);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2206
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2207
		/* no smoke? */
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
  2208
		if ((rvi->railveh_type == RAILVEH_WAGON && effect_type == 0) ||
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2209
				disable_effect ||
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
  2210
				v->vehstatus & VS_HIDDEN) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2211
			continue;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2212
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2213
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2214
		/* 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
  2215
		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
  2216
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2217
		/* No sparks for electric vehicles on nonelectrified tracks */
6480
767cfeae4dbf (svn r8899) -Fix
tron
parents: 6479
diff changeset
  2218
		if (!HasPowerOnRail(v->u.rail.railtype, GetTileRailType(v->tile))) continue;
2612
744aef3af72f (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  2219
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2220
		if (effect_type == 0) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2221
			/* Use default effect type for engine class. */
6074
e70d63ef4d62 (svn r8385) -Fix
tron
parents: 6020
diff changeset
  2222
			effect_type = rvi->engclass;
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2223
		} else {
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2224
			effect_type--;
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2225
		}
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2226
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2227
		int x = _vehicle_smoke_pos[v->direction] * effect_offset;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2228
		int y = _vehicle_smoke_pos[(v->direction + 2) % 8] * effect_offset;
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2229
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  2230
		if (HasBit(v->u.rail.flags, VRF_REVERSE_DIRECTION)) {
3256
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2231
			x = -x;
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2232
			y = -y;
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2233
		}
a23d94f568b7 (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2234
2595
0bab25144829 (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2235
		switch (effect_type) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2236
		case 0:
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2237
			/* steam smoke. */
2612
744aef3af72f (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  2238
			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
  2239
				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
  2240
				sound = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2241
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2242
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2243
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2244
		case 1:
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2245
			/* diesel smoke */
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8428
diff changeset
  2246
			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
  2247
				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
  2248
				sound = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2249
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2250
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2251
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2252
		case 2:
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2253
			/* blue spark */
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8428
diff changeset
  2254
			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
  2255
				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
  2256
				sound = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2257
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2258
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2259
		}
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  2260
	} 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
  2261
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2262
	if (sound) PlayVehicleSound(u, VSE_TRAIN_EFFECT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2263
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2264
9046
ed21d7485650 (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 9006
diff changeset
  2265
void Train::PlayLeaveStationSound() const
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2266
{
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2267
	static const SoundFx sfx[] = {
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2268
		SND_04_TRAIN,
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2269
		SND_0A_TRAIN_HORN,
7082
ad39244be5e0 (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium
parents: 7076
diff changeset
  2270
		SND_0A_TRAIN_HORN,
ad39244be5e0 (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium
parents: 7076
diff changeset
  2271
		SND_47_MAGLEV_2,
ad39244be5e0 (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium
parents: 7076
diff changeset
  2272
		SND_41_MAGLEV
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2273
	};
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2274
9046
ed21d7485650 (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 9006
diff changeset
  2275
	if (PlayVehicleSound(this, VSE_START)) return;
ed21d7485650 (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 9006
diff changeset
  2276
ed21d7485650 (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 9006
diff changeset
  2277
	EngineID engtype = this->engine_type;
ed21d7485650 (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 9006
diff changeset
  2278
	SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], this);
7089
7230d3e22a5f (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 7088
diff changeset
  2279
}
7230d3e22a5f (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 7088
diff changeset
  2280
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2281
static bool CheckTrainStayInDepot(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2282
{
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2283
	/* bail out if not all wagons are in the same depot or not in a depot at all */
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  2284
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  2285
		if (u->u.rail.track != TRACK_BIT_DEPOT || u->tile != v->tile) return false;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2286
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2287
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2288
	/* if the train got no power, then keep it in the depot */
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
  2289
	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
  2290
		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
  2291
		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
  2292
		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
  2293
	}
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
  2294
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2295
	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
  2296
		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
  2297
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2298
			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
  2299
		}
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2300
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2301
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2302
8796
81b0d6b54a1f (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8779
diff changeset
  2303
		if (UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner)) {
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
  2304
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2305
			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
  2306
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2307
	}
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
  2308
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
  2309
	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
  2310
	InvalidateWindowClasses(WC_TRAINS_LIST);
9046
ed21d7485650 (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 9006
diff changeset
  2311
	v->PlayLeaveStationSound();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2312
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  2313
	v->u.rail.track = TRACK_BIT_X;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  2314
	if (v->direction & 2) v->u.rail.track = TRACK_BIT_Y;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2315
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2316
	v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2317
	v->cur_speed = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2318
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7050
diff changeset
  2319
	v->UpdateDeltaXY(v->direction);
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7605
diff changeset
  2320
	v->cur_image = v->GetImage(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2321
	VehiclePositionChanged(v);
8796
81b0d6b54a1f (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8779
diff changeset
  2322
	UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2323
	UpdateTrainAcceleration(v);
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  2324
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2325
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2326
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2327
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2328
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2329
/* Check for station tiles */
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2330
struct TrainTrackFollowerData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2331
	TileIndex dest_coords;
1551
eb05e6a90e67 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  2332
	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
  2333
	uint best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2334
	uint best_track_dist;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  2335
	TrackdirByte best_track;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2336
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2337
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  2338
static bool NtpCallbFindStation(TileIndex tile, TrainTrackFollowerData *ttfd, Trackdir track, uint length)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  2339
{
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2340
	/* heading for nowhere? */
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2928
diff changeset
  2341
	if (ttfd->dest_coords == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2342
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2343
	/* 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
  2344
	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
  2345
				IsTileType(tile, MP_STATION) &&
3334
b72ac8637a30 (svn r4113) Moved all relevant map functions for stations to station_map.[ch]
celestar
parents: 3322
diff changeset
  2346
				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
  2347
				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
  2348
			)) {
2044
68ec4a2f2d79 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2349
		/* 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
  2350
		 * 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
  2351
		 * approximation of where the station is */
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2352
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2353
		/* 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
  2354
		ttfd->best_track = track;
6629
b35b983c6cdb (svn r9146) -Fix [NTP][FS#676](r6800): "Train is lost" message is generated incorrectly. Forgot to set the best bird distance to zero in case we found the destination to indicate success. (de_zeurkous)
KUDr
parents: 6589
diff changeset
  2355
		ttfd->best_bird_dist = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2356
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2357
	} else {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2358
		/* didn't find station, keep track of the best path so far. */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2359
		uint dist = DistanceManhattan(tile, ttfd->dest_coords);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2360
		if (dist < ttfd->best_bird_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2361
			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
  2362
			ttfd->best_track = track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2363
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2364
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2365
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2366
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2367
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  2368
static void FillWithStationData(TrainTrackFollowerData* fd, const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2369
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2370
	fd->dest_coords = v->dest_tile;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2371
	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
  2372
		fd->station_index = v->current_order.dest;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2373
	} else {
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2374
		fd->station_index = INVALID_STATION;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2375
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2376
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2377
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2378
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
  2379
{{ 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
  2380
{{  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
  2381
{{  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
  2382
{{ 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
  2383
{{ 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
  2384
{{  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
  2385
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2386
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2387
static const uint32 _reachable_tracks[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2388
	0x10091009,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2389
	0x00160016,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2390
	0x05200520,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2391
	0x2A002A00,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2392
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2393
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2394
static const byte _search_directions[6][4] = {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2395
	{ 0, 9, 2, 9 }, ///< track 1
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2396
	{ 9, 1, 9, 3 }, ///< track 2
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2397
	{ 9, 0, 3, 9 }, ///< track upper
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2398
	{ 1, 9, 9, 2 }, ///< track lower
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2399
	{ 3, 2, 9, 9 }, ///< track left
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2400
	{ 9, 9, 1, 0 }, ///< track right
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2401
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2402
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2403
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
  2404
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2405
/* choose a track */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  2406
static Track ChooseTrainTrack(Vehicle* v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2407
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  2408
	Track best_track;
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2409
	/* pathfinders are able to tell that route was only 'guessed' */
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
  2410
	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
  2411
2758
9cd8a21e9a7b (svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents: 2752
diff changeset
  2412
#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
  2413
	TIC()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2414
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2415
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 8298
diff changeset
  2416
	assert((tracks & ~TRACK_BIT_MASK) == 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2417
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2418
	/* quick return in case only one possible track is available */
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 8298
diff changeset
  2419
	if (KillFirstBit(tracks) == TRACK_BIT_NONE) return FindFirstTrack(tracks);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2420
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2421
	switch (_patches.pathfinder_for_trains) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2422
		case VPF_YAPF: { /* YAPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2423
			Trackdir trackdir = YapfChooseRailTrack(v, tile, enterdir, tracks, &path_not_found);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2424
			if (trackdir != INVALID_TRACKDIR) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2425
				best_track = TrackdirToTrack(trackdir);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2426
			} else {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2427
				best_track = FindFirstTrack(tracks);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2428
			}
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2429
		} break;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2430
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2431
		case VPF_NPF: { /* NPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2432
			void *perf = NpfBeginInterval();
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2433
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2434
			NPFFindStationOrTileData fstd;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2435
			NPFFillWithOrderData(&fstd, v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2436
			/* The enterdir for the new tile, is the exitdir for the old tile */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2437
			Trackdir trackdir = GetVehicleTrackdir(v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2438
			assert(trackdir != INVALID_TRACKDIR);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2439
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2440
			NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2441
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2442
			if (ftd.best_trackdir == INVALID_TRACKDIR) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2443
				/* We are already at our target. Just do something
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2444
				 * @todo maybe display error?
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2445
				 * @todo: go straight ahead if possible? */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2446
				best_track = FindFirstTrack(tracks);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2447
			} else {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2448
				/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2449
				 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2450
				 * we did not find our target, but ftd.best_trackdir contains the direction leading
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2451
				 * to the tile closest to our target. */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2452
				if (ftd.best_bird_dist != 0) path_not_found = true;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2453
				/* Discard enterdir information, making it a normal track */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2454
				best_track = TrackdirToTrack(ftd.best_trackdir);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2455
			}
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2456
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2457
			int time = NpfEndInterval(perf);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2458
			DEBUG(yapf, 4, "[NPFT] %d us - %d rounds - %d open - %d closed -- ", time, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2459
		} break;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2460
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2461
		default:
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2462
		case VPF_NTP: { /* NTP */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2463
			void *perf = NpfBeginInterval();
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2464
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2465
			TrainTrackFollowerData fd;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2466
			FillWithStationData(&fd, v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2467
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2468
			/* New train pathfinding */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2469
			fd.best_bird_dist = UINT_MAX;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2470
			fd.best_track_dist = UINT_MAX;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2471
			fd.best_track = INVALID_TRACKDIR;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2472
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2473
			NewTrainPathfind(tile - TileOffsByDiagDir(enterdir), v->dest_tile,
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2474
				v->u.rail.compatible_railtypes, enterdir, (NTPEnumProc*)NtpCallbFindStation, &fd);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2475
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2476
			/* check whether the path was found or only 'guessed' */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2477
			if (fd.best_bird_dist != 0) path_not_found = true;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2478
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2479
			if (fd.best_track == INVALID_TRACKDIR) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2480
				/* blaha */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2481
				best_track = FindFirstTrack(tracks);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2482
			} else {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2483
				best_track = TrackdirToTrack(fd.best_track);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2484
			}
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2485
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2486
			int time = NpfEndInterval(perf);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2487
			DEBUG(yapf, 4, "[NTPT] %d us - %d rounds - %d open - %d closed -- ", time, 0, 0, 0);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2488
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2489
	}
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2490
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2491
	/* handle "path not found" state */
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
  2492
	if (path_not_found) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2493
		/* PF didn't find the route */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  2494
		if (!HasBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2495
			/* it is first time the problem occurred, set the "path not found" flag */
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
  2496
			SetBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION);
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2497
			/* 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
  2498
			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
  2499
				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
  2500
				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
  2501
					STR_TRAIN_IS_LOST,
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9208
diff changeset
  2502
					NM_SMALL, NF_VIEWPORT | NF_VEHICLE, NT_ADVICE, DNC_NONE,
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
  2503
					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
  2504
					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
  2505
			}
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
  2506
		}
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
  2507
	} else {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2508
		/* route found, is the train marked with "path not found" flag? */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  2509
		if (HasBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2510
			/* clear the flag as the PF's problem was solved */
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  2511
			ClrBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION);
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2512
			/* can we also delete the "News" item somehow? */
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
  2513
		}
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
  2514
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2515
2758
9cd8a21e9a7b (svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents: 2752
diff changeset
  2516
#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
  2517
	TOC("PF time = ", 1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2518
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2519
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2520
	return best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2521
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2522
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2523
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2524
static bool CheckReverseTrain(Vehicle *v)
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
	if (_opt.diff.line_reverse_mode != 0 ||
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  2527
			v->u.rail.track == TRACK_BIT_DEPOT || v->u.rail.track == TRACK_BIT_WORMHOLE ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2528
			!(v->direction & 1))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2529
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2530
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2531
	TrainTrackFollowerData fd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2532
	FillWithStationData(&fd, v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2533
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2534
	uint reverse_best = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2535
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2536
	assert(v->u.rail.track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2537
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2538
	int 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
  2539
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2540
	switch (_patches.pathfinder_for_trains) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2541
		case VPF_YAPF: { /* YAPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2542
			reverse_best = YapfCheckReverseTrain(v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2543
		} break;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2544
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2545
		case VPF_NPF: { /* NPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2546
			NPFFindStationOrTileData fstd;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2547
			NPFFoundTargetData ftd;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2548
			Vehicle* last = GetLastVehicleInChain(v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2549
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2550
			NPFFillWithOrderData(&fstd, v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2551
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2552
			Trackdir trackdir = GetVehicleTrackdir(v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2553
			Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2554
			assert(trackdir != INVALID_TRACKDIR);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2555
			assert(trackdir_rev != INVALID_TRACKDIR);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2556
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2557
			ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2558
			if (ftd.best_bird_dist != 0) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2559
				/* We didn't find anything, just keep on going straight ahead */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2560
				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
  2561
			} else {
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2562
				if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2563
					reverse_best = true;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2564
				} else {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2565
					reverse_best = false;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2566
				}
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
  2567
			}
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2568
		} break;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2569
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2570
		default:
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2571
		case VPF_NTP: { /* NTP */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2572
			int best_track = -1;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2573
			uint reverse = 0;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2574
			uint best_bird_dist  = 0;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2575
			uint best_track_dist = 0;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2576
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2577
			for (;;) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2578
				fd.best_bird_dist = UINT_MAX;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2579
				fd.best_track_dist = UINT_MAX;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2580
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2581
				NewTrainPathfind(v->tile, v->dest_tile, v->u.rail.compatible_railtypes, (DiagDirection)(reverse ^ i), (NTPEnumProc*)NtpCallbFindStation, &fd);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2582
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2583
				if (best_track != -1) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2584
					if (best_bird_dist != 0) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2585
						if (fd.best_bird_dist != 0) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2586
							/* neither reached the destination, pick the one with the smallest bird dist */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2587
							if (fd.best_bird_dist > best_bird_dist) goto bad;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2588
							if (fd.best_bird_dist < best_bird_dist) goto good;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2589
						} else {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2590
							/* we found the destination for the first time */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2591
							goto good;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2592
						}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2593
					} else {
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2594
						if (fd.best_bird_dist != 0) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2595
							/* didn't find destination, but we've found the destination previously */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2596
							goto bad;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2597
						} else {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2598
							/* both old & new reached the destination, compare track length */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2599
							if (fd.best_track_dist > best_track_dist) goto bad;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2600
							if (fd.best_track_dist < best_track_dist) goto good;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2601
						}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2602
					}
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2603
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2604
					/* if we reach this position, there's two paths of equal value so far.
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2605
					 * pick one randomly. */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2606
					int r = GB(Random(), 0, 8);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2607
					if (_pick_track_table[i] == (v->direction & 3)) r += 80;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2608
					if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2609
					if (r <= 127) goto bad;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2610
				}
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2611
good:;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2612
				best_track = i;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2613
				best_bird_dist = fd.best_bird_dist;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2614
				best_track_dist = fd.best_track_dist;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2615
				reverse_best = reverse;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2616
bad:;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2617
				if (reverse != 0) break;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2618
				reverse = 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2619
			}
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  2620
		} break;
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2623
	return reverse_best != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2624
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2625
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2626
static bool ProcessTrainOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2627
{
3005
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2628
	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
  2629
		case OT_GOTO_DEPOT:
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  2630
			if (!(v->current_order.flags & OFB_PART_OF_ORDERS)) return false;
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  2631
			if ((v->current_order.flags & OFB_SERVICE_IF_NEEDED) &&
3005
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2632
					!VehicleNeedsService(v)) {
7566
435e3c4dbf28 (svn r10335) -Fix (r10331): Increment the current order index *after* using it in UpdateVehicleTimetable.
maedhros
parents: 7548
diff changeset
  2633
				UpdateVehicleTimetable(v, true);
3005
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2634
				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
  2635
			}
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2636
			break;
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2637
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2638
		case OT_LOADING:
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2639
		case OT_LEAVESTATION:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2640
			return false;
4351
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
  2641
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
  2642
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2643
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2644
7605
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2645
	/**
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2646
	 * Reversing because of order change is allowed only just after leaving a
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2647
	 * station (and the difficulty setting to allowed, of course)
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2648
	 * this can be detected because only after OT_LEAVESTATION, current_order
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2649
	 * will be reset to nothing. (That also happens if no order, but in that case
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2650
	 * it won't hit the point in code where may_reverse is checked)
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2651
	 */
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2652
	bool may_reverse = v->current_order.type == OT_NOTHING;
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2653
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2654
	/* check if we've reached the waypoint? */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2655
	if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) {
7566
435e3c4dbf28 (svn r10335) -Fix (r10331): Increment the current order index *after* using it in UpdateVehicleTimetable.
maedhros
parents: 7548
diff changeset
  2656
		UpdateVehicleTimetable(v, true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2657
		v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2658
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2659
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2660
	/* 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
  2661
	if (_patches.new_nonstop &&
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  2662
			v->current_order.flags & OFB_NON_STOP &&
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2663
			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
  2664
			v->current_order.dest == GetStationIndex(v->tile)) {
7566
435e3c4dbf28 (svn r10335) -Fix (r10331): Increment the current order index *after* using it in UpdateVehicleTimetable.
maedhros
parents: 7548
diff changeset
  2665
		UpdateVehicleTimetable(v, true);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2666
		v->cur_order_index++;
0
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
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2669
	/* Get the current order */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2670
	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
  2671
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2672
	const Order *order = GetVehicleOrder(v, v->cur_order_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2673
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2674
	/* 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
  2675
	if (order == NULL) {
6589
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  2676
		v->current_order.Free();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2677
		v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2678
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2679
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2680
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2681
	/* 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
  2682
	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
  2683
			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
  2684
			order->dest  == v->current_order.dest)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2685
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2686
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2687
	/* 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
  2688
	v->current_order = *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2689
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2690
	v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2691
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
  2692
	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
  2693
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2694
	switch (order->type) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2695
		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
  2696
			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
  2697
				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
  2698
			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
  2699
			break;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2700
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2701
		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
  2702
			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
  2703
			break;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2704
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2705
		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
  2706
			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
  2707
			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
  2708
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
  2709
		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
  2710
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2711
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2712
7605
5135b6db22bf (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7592
diff changeset
  2713
	return may_reverse && CheckReverseTrain(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2714
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2715
7049
01825af2ce90 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 7048
diff changeset
  2716
void Train::MarkDirty()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2717
{
7049
01825af2ce90 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 7048
diff changeset
  2718
	Vehicle *v = this;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2719
	do {
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7605
diff changeset
  2720
		v->cur_image = v->GetImage(v->direction);
8813
6d054db96ede (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8808
diff changeset
  2721
		MarkSingleVehicleDirty(v);
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  2722
	} while ((v = v->Next()) != NULL);
7049
01825af2ce90 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 7048
diff changeset
  2723
01825af2ce90 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 7048
diff changeset
  2724
	/* need to update acceleration and cached values since the goods on the train changed. */
01825af2ce90 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 7048
diff changeset
  2725
	TrainCargoChanged(this);
01825af2ce90 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 7048
diff changeset
  2726
	UpdateTrainAcceleration(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2727
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2728
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2729
static int UpdateTrainSpeed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2730
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2731
	uint accel;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2732
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  2733
	if (v->vehstatus & VS_STOPPED || HasBit(v->u.rail.flags, VRF_REVERSING)) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2734
		if (_patches.realistic_acceleration) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2735
			accel = GetTrainAcceleration(v, AM_BRAKE) * 2;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2736
		} else {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2737
			accel = v->acceleration * -2;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2738
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2739
	} else {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2740
		if (_patches.realistic_acceleration) {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2741
			accel = GetTrainAcceleration(v, AM_ACCEL);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2742
		} else {
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2743
			accel = v->acceleration;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2744
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2745
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2746
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2747
	uint spd = v->subspeed + accel * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2748
	v->subspeed = (byte)spd;
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2749
	{
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2750
		int tempmax = v->max_speed;
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2751
		if (v->cur_speed > v->max_speed)
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2752
			tempmax = v->cur_speed - (v->cur_speed / 10) - 1;
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8377
diff changeset
  2753
		v->cur_speed = spd = Clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax);
1179
abea5b669f74 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2754
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2755
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2756
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2757
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2758
	spd += v->progress;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2759
	v->progress = (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2760
	return (spd >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2761
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2762
1551
eb05e6a90e67 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  2763
static void TrainEnterStation(Vehicle *v, StationID station)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2764
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2765
	v->last_station_visited = station;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2766
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2767
	/* check if a train ever visited this station before */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2768
	Station *st = GetStation(station);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2769
	if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2770
		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
  2771
		SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2772
		AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2773
			STR_8801_CITIZENS_CELEBRATE_FIRST,
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9208
diff changeset
  2774
			NM_THIN, NF_VIEWPORT | NF_VEHICLE, v->owner == _local_player ? NT_ARRIVAL_PLAYER : NT_ARRIVAL_OTHER, DNC_NONE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2775
			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
  2776
			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
  2777
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2778
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2779
7046
5f11f759f74d (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 7042
diff changeset
  2780
	v->BeginLoading();
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
  2781
	v->current_order.dest = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2782
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2783
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2784
static byte AfterSetTrainPos(Vehicle *v, bool new_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2785
{
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2786
	byte old_z = v->z_pos;
7575
0e0efc0fbf2d (svn r10344) -Fix (r10317): confusion between TRACK_n and TRACK_BIT_n stopped NW/SE slopes being picked up, and compare middle of tile against current z, not previous.
peter1138
parents: 7566
diff changeset
  2787
	v->z_pos = GetSlopeZ(v->x_pos, v->y_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2788
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2789
	if (new_tile) {
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  2790
		ClrBit(v->u.rail.flags, VRF_GOINGUP);
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  2791
		ClrBit(v->u.rail.flags, VRF_GOINGDOWN);
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2792
7575
0e0efc0fbf2d (svn r10344) -Fix (r10317): confusion between TRACK_n and TRACK_BIT_n stopped NW/SE slopes being picked up, and compare middle of tile against current z, not previous.
peter1138
parents: 7566
diff changeset
  2793
		if (v->u.rail.track == TRACK_BIT_X || v->u.rail.track == TRACK_BIT_Y) {
0e0efc0fbf2d (svn r10344) -Fix (r10317): confusion between TRACK_n and TRACK_BIT_n stopped NW/SE slopes being picked up, and compare middle of tile against current z, not previous.
peter1138
parents: 7566
diff changeset
  2794
			/* Any track that isn't TRACK_BIT_X or TRACK_BIT_Y cannot be sloped.
7548
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2795
			 * To check whether the current tile is sloped, and in which
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2796
			 * direction it is sloped, we get the 'z' at the center of
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2797
			 * the tile (middle_z) and the edge of the tile (old_z),
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2798
			 * which we then can compare. */
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2799
			static const int HALF_TILE_SIZE = TILE_SIZE / 2;
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2800
			static const int INV_TILE_SIZE_MASK = ~(TILE_SIZE - 1);
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2801
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2802
			byte middle_z = GetSlopeZ((v->x_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE, (v->y_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE);
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2803
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2804
			/* For some reason tunnel tiles are always given as sloped :(
6139cecdb002 (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7520
diff changeset
  2805
			 * But they are not sloped... */
7575
0e0efc0fbf2d (svn r10344) -Fix (r10317): confusion between TRACK_n and TRACK_BIT_n stopped NW/SE slopes being picked up, and compare middle of tile against current z, not previous.
peter1138
parents: 7566
diff changeset
  2806
			if (middle_z != v->z_pos && !IsTunnelTile(TileVirtXY(v->x_pos, v->y_pos))) {
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
  2807
				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
  2808
			}
954
4d052517a993 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2809
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2810
	}
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
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2813
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2814
	return old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2815
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2816
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  2817
static const Direction _new_vehicle_direction_table[11] = {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  2818
	DIR_N , DIR_NW, DIR_W , INVALID_DIR,
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  2819
	DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  2820
	DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2821
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2822
9103
ef4a10ed7f9f (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 9102
diff changeset
  2823
static inline Direction GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2824
{
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
  2825
	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
  2826
							TileX(new_tile) - TileX(old_tile) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2827
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2828
	return _new_vehicle_direction_table[offs];
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
9103
ef4a10ed7f9f (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 9102
diff changeset
  2831
static inline 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
  2832
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2833
	byte offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2834
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2835
	x -= v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2836
	if (x >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2837
		offs = (x > 2) ? 0 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2838
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2839
		offs = (x < -2) ? 2 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2840
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2841
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2842
	y -= v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2843
	if (y >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2844
		offs += ((y > 2) ? 0 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2845
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2846
		offs += ((y < -2) ? 2 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2847
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2848
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2849
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2850
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2851
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2852
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2853
/* Check if the vehicle is compatible with the specified tile */
9103
ef4a10ed7f9f (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 9102
diff changeset
  2854
static inline bool CheckCompatibleRail(const Vehicle *v, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2855
{
1048
edfc783f241d (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2856
	return
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2857
		IsTileOwner(tile, v->owner) && (
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  2858
			!IsFrontEngine(v) ||
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  2859
			HasBit(v->u.rail.compatible_railtypes, GetRailType(tile))
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2860
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2861
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2862
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2863
struct RailtypeSlowdownParams {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2864
	byte small_turn, large_turn;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2865
	byte z_up; // fraction to remove when moving up
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2866
	byte z_down; // fraction to remove when moving down
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  2867
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2868
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
  2869
static const RailtypeSlowdownParams _railtype_slowdown[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2870
	// normal accel
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2871
	{256 / 4, 256 / 2, 256 / 4, 2}, ///< normal
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2872
	{256 / 4, 256 / 2, 256 / 4, 2}, ///< electrified
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2873
	{256 / 4, 256 / 2, 256 / 4, 2}, ///< monorail
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2874
	{0,       256 / 2, 256 / 4, 2}, ///< maglev
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2875
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2876
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2877
/** Modify the speed of the vehicle due to a turn */
9103
ef4a10ed7f9f (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 9102
diff changeset
  2878
static inline void AffectSpeedByDirChange(Vehicle* v, Direction new_dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2879
{
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2880
	if (_patches.realistic_acceleration) return;
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2881
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2882
	DirDiff diff = DirDifference(v->direction, new_dir);
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2883
	if (diff == DIRDIFF_SAME) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2884
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2885
	const RailtypeSlowdownParams *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
  2886
	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
  2887
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2888
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2889
/** Modify the speed of the vehicle due to a change in altitude */
9103
ef4a10ed7f9f (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 9102
diff changeset
  2890
static inline void AffectSpeedByZChange(Vehicle *v, byte old_z)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2891
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2892
	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
  2893
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  2894
	const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->u.rail.railtype];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2895
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2896
	if (old_z < v->z_pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2897
		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
  2898
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2899
		uint16 spd = v->cur_speed + rsp->z_down;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2900
		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
  2901
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2902
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2903
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
  2904
static const DiagDirection _otherside_signal_directions[] = {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  2905
	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, INVALID_DIAGDIR, INVALID_DIAGDIR,
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
  2906
	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
  2907
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2908
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  2909
static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2910
{
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
  2911
	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
  2912
			GetRailTileType(tile) == RAIL_TILE_SIGNALS) {
3269
95e7bffcb3c5 (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2913
		uint i = FindFirstBit2x64(GetTrackBits(tile) * 0x101 & _reachable_tracks[dir]);
8796
81b0d6b54a1f (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8779
diff changeset
  2914
		UpdateSignalsOnSegment(tile, _otherside_signal_directions[i], GetTileOwner(tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2915
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2916
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2917
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2918
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2919
static void SetVehicleCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2920
{
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
  2921
	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
  2922
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  2923
	/* we may need to update crossing we were approaching */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  2924
	TileIndex crossing = TrainApproachingCrossingTile(v);
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  2925
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2926
	v->u.rail.crash_anim_pos++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2927
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  2928
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
8767
2315691f8340 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8766
diff changeset
  2929
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
2315691f8340 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8766
diff changeset
  2930
2315691f8340 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8766
diff changeset
  2931
	if (v->u.rail.track == TRACK_BIT_DEPOT) {
2315691f8340 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8766
diff changeset
  2932
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
2315691f8340 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8766
diff changeset
  2933
	}
2315691f8340 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8766
diff changeset
  2934
2315691f8340 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8766
diff changeset
  2935
	RebuildVehicleLists();
2315691f8340 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8766
diff changeset
  2936
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2937
	BEGIN_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2938
		v->vehstatus |= VS_CRASHED;
8813
6d054db96ede (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8808
diff changeset
  2939
		MarkSingleVehicleDirty(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2940
	END_ENUM_WAGONS(v)
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  2941
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  2942
	/* must be updated after the train has been marked crashed */
8838
1549b7f9d0a8 (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8830
diff changeset
  2943
	if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2944
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2945
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2946
static uint CountPassengersInTrain(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2947
{
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2948
	uint num = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2949
	BEGIN_ENUM_WAGONS(v)
7506
e52d89f5c7c1 (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7498
diff changeset
  2950
		if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) num += v->cargo.Count();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2951
	END_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2952
	return num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2953
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2954
7462
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2955
struct TrainCollideChecker {
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2956
	Vehicle *v;
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2957
	uint num;
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2958
};
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2959
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2960
static void *FindTrainCollideEnum(Vehicle *v, void *data)
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2961
{
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2962
	TrainCollideChecker* tcc = (TrainCollideChecker*)data;
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2963
8777
51c8adbd661b (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8767
diff changeset
  2964
	if (v->type != VEH_TRAIN) return NULL;
51c8adbd661b (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8767
diff changeset
  2965
51c8adbd661b (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8767
diff changeset
  2966
	/* get first vehicle now to make most usual checks faster */
51c8adbd661b (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8767
diff changeset
  2967
	Vehicle *coll = v->First();
51c8adbd661b (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8767
diff changeset
  2968
8808
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  2969
	/* can't collide with own wagons && can't crash in depot && the same height level */
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  2970
	if (coll != tcc->v && v->u.rail.track != TRACK_BIT_DEPOT && abs(v->z_pos - tcc->v->z_pos) < 6) {
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  2971
		int x_diff = v->x_pos - tcc->v->x_pos;
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  2972
		int y_diff = v->y_pos - tcc->v->y_pos;
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  2973
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  2974
		/* needed to disable possible crash of competitor train in station by building diagonal track at its end */
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  2975
		if (x_diff * x_diff + y_diff * y_diff > 25) return NULL;
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  2976
7462
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2977
		if (!(tcc->v->vehstatus & VS_CRASHED)) {
8808
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  2978
			/* two drivers + passengers killed in train tcc->v (if it was not crashed already) */
7462
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2979
			tcc->num += 2 + CountPassengersInTrain(tcc->v);
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2980
			SetVehicleCrashed(tcc->v);
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2981
		}
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2982
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2983
		if (!(coll->vehstatus & VS_CRASHED)) {
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2984
			/* two drivers + passengers killed in train coll (if it was not crashed already) */
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2985
			tcc->num += 2 + CountPassengersInTrain(coll);
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2986
			SetVehicleCrashed(coll);
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2987
		}
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2988
	}
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2989
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2990
	return NULL;
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2991
}
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  2992
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  2993
/**
1434
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2994
 * 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
  2995
 * 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
  2996
 * 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
  2997
 * plays a sound.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2998
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2999
static void CheckTrainCollision(Vehicle *v)
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 */
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  3002
	if (v->u.rail.track == TRACK_BIT_DEPOT) return;
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  3003
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  3004
	assert(v->u.rail.track == TRACK_BIT_WORMHOLE || 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
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3006
	TrainCollideChecker tcc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3007
	tcc.v = v;
7462
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  3008
	tcc.num = 0;
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  3009
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  3010
	/* find colliding vehicles */
7867
07a2830337ce (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7830
diff changeset
  3011
	if (v->u.rail.track == TRACK_BIT_WORMHOLE) {
07a2830337ce (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7830
diff changeset
  3012
		VehicleFromPos(v->tile, &tcc, FindTrainCollideEnum);
8693
3d721429cc4f (svn r11760) -Codechange: unify the way how other end of a tunnel/bridge is determined at some places
smatz
parents: 8674
diff changeset
  3013
		VehicleFromPos(GetOtherTunnelBridgeEnd(v->tile), &tcc, FindTrainCollideEnum);
7867
07a2830337ce (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7830
diff changeset
  3014
	} else {
07a2830337ce (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7830
diff changeset
  3015
		VehicleFromPosXY(v->x_pos, v->y_pos, &tcc, FindTrainCollideEnum);
07a2830337ce (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7830
diff changeset
  3016
	}
7462
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  3017
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  3018
	/* any dead -> no crash */
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  3019
	if (tcc.num == 0) return;
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  3020
08e10afded56 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138
parents: 7449
diff changeset
  3021
	SetDParam(0, tcc.num);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3022
	AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL,
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9208
diff changeset
  3023
		NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ACCIDENT, DNC_NONE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3024
		v->index,
1434
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3025
		0
f219d7bf2eb6 (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3026
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3027
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3028
	ModifyStationRatingAround(v->tile, v->owner, -160, 30);
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 540
diff changeset
  3029
	SndPlayVehicleFx(SND_13_BIG_CRASH, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3030
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3031
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
{
8577
fc4b7dd34058 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8569
diff changeset
  3034
	Direction dir = *(Direction*)data;
fc4b7dd34058 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8569
diff changeset
  3035
fc4b7dd34058 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8569
diff changeset
  3036
	if (v->type == VEH_TRAIN && IsFrontEngine(v)) {
fc4b7dd34058 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8569
diff changeset
  3037
		DirDiff diff = ChangeDirDiff(DirDifference(v->direction, dir), DIRDIFF_90RIGHT);
3158
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
9206
12d35b56014c (svn r12384) -Fix (r2428): do not disconnect train when reversing, it may 'think' it is whole in a depot
smatz
parents: 9202
diff changeset
  3044
static void TrainController(Vehicle *v, Vehicle *nomove, 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
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3048
	/* For every vehicle after and including the given vehicle */
9206
12d35b56014c (svn r12384) -Fix (r2428): do not disconnect train when reversing, it may 'think' it is whole in a depot
smatz
parents: 9202
diff changeset
  3049
	for (prev = v->Previous(); v != nomove; prev = v, v = v->Next()) {
7367
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3050
		DiagDirection enterdir = DIAGDIR_BEGIN;
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3051
		bool update_signals_crossing = false; // will we update signals or crossing state?
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3052
		BeginVehicleMove(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3053
6479
b228a94a1832 (svn r8897) -Fix
tron
parents: 6478
diff changeset
  3054
		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  3055
		if (v->u.rail.track != TRACK_BIT_WORMHOLE) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3056
			/* Not inside tunnel */
6478
817c29f1882b (svn r8896) -Fix
tron
parents: 6477
diff changeset
  3057
			if (gp.old_tile == gp.new_tile) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3058
				/* Staying in the old tile */
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  3059
				if (v->u.rail.track == TRACK_BIT_DEPOT) {
6320
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3060
					/* Inside depot */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3061
					gp.x = v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3062
					gp.y = v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3063
				} else {
6320
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3064
					/* Not inside depot */
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3065
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
  3066
					if (IsFrontEngine(v) && !TrainCheckIfLineEnds(v)) return;
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3067
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3068
					uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  3069
					if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3070
						goto invalid_rail;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3071
					}
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  3072
					if (HasBit(r, VETS_ENTERED_STATION)) {
6317
c73bda71ac16 (svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents: 6195
diff changeset
  3073
						TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3074
						return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3075
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3076
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3077
					if (v->current_order.type == OT_LEAVESTATION) {
6589
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  3078
						v->current_order.Free();
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  3079
						InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3080
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3081
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3082
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3083
				/* A new tile is about to be entered. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3084
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3085
				/* Determine what direction we're entering the new tile from */
6477
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3086
				Direction dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
7367
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3087
				enterdir = DirToDiagDir(dir);
6320
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3088
				assert(IsValidDiagDirection(enterdir));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3089
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3090
				/* 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
  3091
				 * away the bits that aren't reachable. */
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3092
				TrackStatus ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL, 0, ReverseDiagDir(enterdir)) & _reachable_tracks[enterdir];
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3093
				TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts);
9116
6c4b16c2ebea (svn r12203) -Fix (r12199): Make trains pay attention to signals again.
frosch
parents: 9112
diff changeset
  3094
				TrackBits red_signals = TrackdirBitsToTrackBits(TrackStatusToRedSignals(ts));
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3095
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3096
				TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  3097
				if (_patches.pathfinder_for_trains != VPF_NTP && _patches.forbid_90_deg && prev == NULL) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3098
					/* We allow wagons to make 90 deg turns, because forbid_90_deg
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3099
					 * can be switched on halfway a turn */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  3100
					bits &= ~TrackCrossesTracks(FindFirstTrack(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
  3101
				}
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
  3102
6320
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3103
				if (bits == TRACK_BIT_NONE) goto invalid_rail;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3104
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3105
				/* 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
  3106
				 * with the current train, if not, bail out. */
6320
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3107
				if (!CheckCompatibleRail(v, gp.new_tile)) goto invalid_rail;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3108
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3109
				TrackBits chosen_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3110
				if (prev == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3111
					/* 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
  3112
					 * available tracks to choose */
6481
c069e5454782 (svn r8900) -Fix
tron
parents: 6480
diff changeset
  3113
					chosen_track = TrackToTrackBits(ChooseTrainTrack(v, gp.new_tile, enterdir, bits));
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3114
					assert(chosen_track & bits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3115
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3116
					/* Check if it's a red signal and that force proceed is not clicked. */
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3117
					if (red_signals & chosen_track && v->u.rail.force_proceed == 0) {
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3118
						/* In front of a red signal */
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3119
						Trackdir i = FindFirstTrackdir(trackdirbits);
6477
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3120
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3121
						if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) {
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3122
							v->cur_speed = 0;
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3123
							v->subspeed = 0;
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3124
							v->progress = 255 - 100;
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3125
							if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20) return;
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3126
						} else if (HasSignalOnTrackdir(gp.new_tile, i)) {
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3127
							v->cur_speed = 0;
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3128
							v->subspeed = 0;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6986
diff changeset
  3129
							v->progress = 255 - 10;
6477
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3130
							if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) {
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3131
								TileIndex o_tile = gp.new_tile + TileOffsByDiagDir(enterdir);
8577
fc4b7dd34058 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8569
diff changeset
  3132
								Direction rdir = ReverseDir(dir);
6477
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3133
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3134
								/* check if a train is waiting on the other side */
8577
fc4b7dd34058 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8569
diff changeset
  3135
								if (VehicleFromPos(o_tile, &rdir, &CheckVehicleAtSignal) == NULL) return;
6477
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3136
							}
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3137
						}
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3138
						goto reverse_train_direction;
c0add302a009 (svn r8894) -Fix
tron
parents: 6476
diff changeset
  3139
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3140
				} else {
6320
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3141
					static const TrackBits _matching_tracks[8] = {
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3142
							TRACK_BIT_LEFT  | TRACK_BIT_RIGHT, TRACK_BIT_X,
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3143
							TRACK_BIT_UPPER | TRACK_BIT_LOWER, TRACK_BIT_Y,
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3144
							TRACK_BIT_LEFT  | TRACK_BIT_RIGHT, TRACK_BIT_X,
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3145
							TRACK_BIT_UPPER | TRACK_BIT_LOWER, TRACK_BIT_Y
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3146
					};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3147
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3148
					/* The wagon is active, simply follow the prev vehicle. */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  3149
					chosen_track = (TrackBits)(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
  3150
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3151
6320
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3152
				/* Make sure chosen track is a valid track */
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3153
				assert(
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3154
						chosen_track == TRACK_BIT_X     || chosen_track == TRACK_BIT_Y ||
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3155
						chosen_track == TRACK_BIT_UPPER || chosen_track == TRACK_BIT_LOWER ||
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
  3156
						chosen_track == TRACK_BIT_LEFT  || chosen_track == TRACK_BIT_RIGHT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3157
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3158
				/* Update XY to reflect the entrance to the new tile, and select the direction to use */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3159
				const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3160
				gp.x = (gp.x & ~0xF) | b[0];
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3161
				gp.y = (gp.y & ~0xF) | b[1];
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3162
				Direction chosen_dir = (Direction)b[2];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3163
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3164
				/* Call the landscape function and tell it that the vehicle entered the tile */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3165
				uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  3166
				if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3167
					goto invalid_rail;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3168
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3169
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3170
				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
  3171
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  3172
				if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3173
					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
  3174
6480
767cfeae4dbf (svn r8899) -Fix
tron
parents: 6479
diff changeset
  3175
					if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_tile)) {
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7989
diff changeset
  3176
						TrainPowerChanged(v->First());
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
  3177
					}
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
  3178
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3179
					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
  3180
					assert(v->u.rail.track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3181
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3182
7367
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3183
				/* We need to update signal status, but after the vehicle position hash
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3184
				 * has been updated by AfterSetTrainPos() */
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3185
				update_signals_crossing = true;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3186
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
  3187
				if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3188
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3189
				v->direction = chosen_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3190
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3191
		} else {
8639
afebb18ecef5 (svn r11705) -Fix [FS#1557]: trains could have sprites with wrong direction when reversing, also was inconsistent with save/load process (possible desyncs)
smatz
parents: 8636
diff changeset
  3192
			/* In a tunnel or on a bridge
afebb18ecef5 (svn r11705) -Fix [FS#1557]: trains could have sprites with wrong direction when reversing, also was inconsistent with save/load process (possible desyncs)
smatz
parents: 8636
diff changeset
  3193
			 * - for tunnels, only the part when the vehicle is not visible (part of enter/exit tile too)
afebb18ecef5 (svn r11705) -Fix [FS#1557]: trains could have sprites with wrong direction when reversing, also was inconsistent with save/load process (possible desyncs)
smatz
parents: 8636
diff changeset
  3194
			 * - for bridges, only the middle part - without the bridge heads */
6467
6228b576422d (svn r8884) -Fix
tron
parents: 6462
diff changeset
  3195
			if (!(v->vehstatus & VS_HIDDEN)) {
6228b576422d (svn r8884) -Fix
tron
parents: 6462
diff changeset
  3196
				v->cur_speed =
8987
782be613237b (svn r12066) -Codechange: Rename GetBridge for the more common GetBridgeSpec
belugas
parents: 8978
diff changeset
  3197
					min(v->cur_speed, GetBridgeSpec(GetBridgeType(v->tile))->speed);
6467
6228b576422d (svn r8884) -Fix
tron
parents: 6462
diff changeset
  3198
			}
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
  3199
8886
9f2c7ebc7fc9 (svn r11960) -Cleanup: simplify some IsTunnel(Tile) / IsBridge(Tile) conditions
smatz
parents: 8852
diff changeset
  3200
			if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3201
				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
  3202
				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
  3203
				VehiclePositionChanged(v);
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
  3204
				if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3205
				continue;
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
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3208
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3209
		/* update image of train, as well as delta XY */
8639
afebb18ecef5 (svn r11705) -Fix [FS#1557]: trains could have sprites with wrong direction when reversing, also was inconsistent with save/load process (possible desyncs)
smatz
parents: 8636
diff changeset
  3210
		v->UpdateDeltaXY(v->direction);
afebb18ecef5 (svn r11705) -Fix [FS#1557]: trains could have sprites with wrong direction when reversing, also was inconsistent with save/load process (possible desyncs)
smatz
parents: 8636
diff changeset
  3211
		if (update_image) v->cur_image = v->GetImage(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3212
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3213
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3214
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3215
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3216
		/* update the Z position of the vehicle */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3217
		byte 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
  3218
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3219
		if (prev == NULL) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3220
			/* This is the first vehicle in the train */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3221
			AffectSpeedByZChange(v, old_z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3222
		}
7367
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3223
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3224
		if (update_signals_crossing) {
7367
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3225
			if (IsFrontEngine(v)) TrainMovedChangeSignals(gp.new_tile, enterdir);
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3226
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3227
			/* Signals can only change when the first
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3228
			 * (above) or the last vehicle moves. */
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3229
			if (v->Next() == NULL) {
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3230
				TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
8838
1549b7f9d0a8 (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8830
diff changeset
  3231
				if (IsLevelCrossingTile(gp.old_tile)) UpdateLevelCrossing(gp.old_tile);
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3232
			}
7367
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
  3233
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3234
	}
1438
d6bf51eff1bc (svn r1942) Fix r1938
tron
parents: 1434
diff changeset
  3235
	return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3236
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3237
invalid_rail:
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3238
	/* We've reached end of line?? */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3239
	if (prev != NULL) error("!Disconnecting train");
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3240
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3241
reverse_train_direction:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3242
	v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3243
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3244
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3245
	ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3246
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3247
1418
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3248
/**
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3249
 * 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
  3250
 * 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
  3251
 * will remove the last wagon of a crashed train. If this wagon was on a crossing,
8569
a6c1cf581822 (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz
parents: 8568
diff changeset
  3252
 * or inside a tunnel/bridge, recalculate the signals as they might need updating
6980
6b5dee376733 (svn r9665) -Documentation: Doxygen corrections,errors, corrections of corrections...
belugas
parents: 6949
diff changeset
  3253
 * @param v the Vehicle of which last wagon is to be removed
1418
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3254
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3255
static void DeleteLastWagon(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3256
{
8763
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3257
	Vehicle *first = v->First();
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3258
1418
1c235eb4197e (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3259
	/* 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
  3260
	 * *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
  3261
	 * one which will physicially be removed */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3262
	Vehicle *u = v;
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  3263
	for (; v->Next() != NULL; v = v->Next()) u = v;
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  3264
	u->SetNext(NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3265
8763
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3266
	if (first == v) {
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3267
		/* Removing front vehicle (the last to go) */
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3268
		DeleteWindowById(WC_VEHICLE_VIEW, v->index);
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3269
		InvalidateWindow(WC_COMPANY, v->owner);
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3270
	} else {
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3271
		/* Recalculate cached train properties */
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3272
		TrainConsistChanged(first);
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3273
		/* Update the depot window if the first vehicle is in depot -
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3274
		 * if v == first, then it is updated in PreDestructor() */
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3275
		if (first->u.rail.track == TRACK_BIT_DEPOT) {
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3276
			InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3277
		}
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3278
	}
19e9841201f8 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8760
diff changeset
  3279
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
8813
6d054db96ede (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8808
diff changeset
  3282
	MarkSingleVehicleDirty(v);
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  3283
8751
249647531939 (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz
parents: 8750
diff changeset
  3284
	/* 'v' shouldn't be accessed after it has been deleted */
249647531939 (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz
parents: 8750
diff changeset
  3285
	TrackBits track = v->u.rail.track;
249647531939 (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz
parents: 8750
diff changeset
  3286
	TileIndex tile = v->tile;
8796
81b0d6b54a1f (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8779
diff changeset
  3287
	Owner owner = v->owner;
8751
249647531939 (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz
parents: 8750
diff changeset
  3288
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7867
diff changeset
  3289
	delete v;
8796
81b0d6b54a1f (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8779
diff changeset
  3290
	v = NULL; // make sure nobody will won't try to read 'v' anymore
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3291
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3292
	/* check if the wagon was on a road/rail-crossing */
8838
1549b7f9d0a8 (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8830
diff changeset
  3293
	if (IsLevelCrossingTile(tile)) UpdateLevelCrossing(tile);
8751
249647531939 (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz
parents: 8750
diff changeset
  3294
8753
33e297b1595d (svn r11821) -Fix (r11802): 'optimization assert' when removing crashed wagon in some cases
smatz
parents: 8752
diff changeset
  3295
	/* Update signals */
33e297b1595d (svn r11821) -Fix (r11802): 'optimization assert' when removing crashed wagon in some cases
smatz
parents: 8752
diff changeset
  3296
	if (IsTileType(tile, MP_TUNNELBRIDGE) || IsTileDepotType(tile, TRANSPORT_RAIL)) {
8796
81b0d6b54a1f (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8779
diff changeset
  3297
		UpdateSignalsOnSegment(tile, INVALID_DIAGDIR, owner);
8753
33e297b1595d (svn r11821) -Fix (r11802): 'optimization assert' when removing crashed wagon in some cases
smatz
parents: 8752
diff changeset
  3298
	} else {
8796
81b0d6b54a1f (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8779
diff changeset
  3299
		SetSignalsOnBothDir(tile, (Track)(FIND_FIRST_BIT(track)), owner);
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  3300
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3301
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3302
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3303
static void ChangeTrainDirRandomly(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3304
{
3160
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3305
	static const DirDiff delta[] = {
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3306
		DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3307
	};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3308
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3309
	do {
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
  3310
		/* We don't need to twist around vehicles if they're not visible */
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
  3311
		if (!(v->vehstatus & VS_HIDDEN)) {
3977
edb5b94e2094 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3312
			v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3313
			BeginVehicleMove(v);
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7050
diff changeset
  3314
			v->UpdateDeltaXY(v->direction);
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7605
diff changeset
  3315
			v->cur_image = v->GetImage(v->direction);
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
  3316
			/* Refrain from updating the z position of the vehicle when on
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
  3317
			   a bridge, because AfterSetTrainPos will put the vehicle under
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
  3318
			   the bridge in that case */
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  3319
			if (v->u.rail.track != TRACK_BIT_WORMHOLE) AfterSetTrainPos(v, false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3320
		}
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  3321
	} while ((v = v->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3322
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3323
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3324
static void HandleCrashedTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3325
{
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
  3326
	int state = ++v->u.rail.crash_anim_pos;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3327
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6318
diff changeset
  3328
	if (state == 4 && !(v->vehstatus & VS_HIDDEN)) {
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
  3329
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3330
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3331
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3332
	uint32 r;
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8428
diff changeset
  3333
	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
  3334
		int index = (r * 10 >> 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3335
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3336
		Vehicle *u = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3337
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3338
			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
  3339
				r = Random();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3340
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3341
				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
  3342
					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
  3343
					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
  3344
					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
  3345
					EV_EXPLOSION_SMALL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3346
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3347
			}
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7984
diff changeset
  3348
		} while ((u = u->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3349
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3350
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
  3351
	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
  3352
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
  3353
	if (state >= 4440 && !(v->tick_counter&0x1F)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3354
		DeleteLastWagon(v);
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7104
diff changeset
  3355
		InvalidateWindow(WC_REPLACE_VEHICLE, (v->group_id << 16) | VEH_TRAIN);
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
  3356
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3357
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3358
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3359
static void HandleBrokenTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3360
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3361
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3362
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3363
		v->cur_speed = 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
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3366
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3367
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3368
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3369
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3370
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  3371
		if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
6683
7ec558346172 (svn r9400) -Codechange: Use some more representative enum names for landscape types.
belugas
parents: 6660
diff changeset
  3372
			SndPlayVehicleFx((_opt.landscape != LT_TOYLAND) ?
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  3373
				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
  3374
		}
0
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->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3377
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
7830
210381b46ae6 (svn r10697) -Codechange: give a more sensible names to some of the unkX variables.
rubidium
parents: 7762
diff changeset
  3378
			if (u != NULL) u->u.special.animation_state = v->breakdown_delay * 2;
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3381
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3382
	if (!(v->tick_counter & 3)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3383
		if (!--v->breakdown_delay) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3384
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3385
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3386
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3387
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3388
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3389
8779
efb41d093a39 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz
parents: 8777
diff changeset
  3390
/** Maximum speeds for train that is broken down or approaching line end */
8808
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  3391
static const uint16 _breakdown_speeds[16] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3392
	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
  3393
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3394
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3395
8779
efb41d093a39 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz
parents: 8777
diff changeset
  3396
/**
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3397
 * Train is approaching line end, slow down and possibly reverse
8779
efb41d093a39 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz
parents: 8777
diff changeset
  3398
 *
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3399
 * @param v front train engine
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3400
 * @param signal not line end, just a red signal
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3401
 * @return true iff we did NOT have to reverse
8779
efb41d093a39 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz
parents: 8777
diff changeset
  3402
 */
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3403
static bool TrainApproachingLineEnd(Vehicle *v, bool signal)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3404
{
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3405
	/* Calc position within the current tile */
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3406
	uint x = v->x_pos & 0xF;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3407
	uint y = v->y_pos & 0xF;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3408
8808
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  3409
	/* for diagonal directions, 'x' will be 0..15 -
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  3410
	 * for other directions, it will be 1, 3, 5, ..., 15 */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3411
	switch (v->direction) {
8808
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  3412
		case DIR_N : x = ~x + ~y + 25; break;
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3413
		case DIR_NW: x = y;            /* FALLTHROUGH */
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3414
		case DIR_NE: x = ~x + 16;      break;
8808
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  3415
		case DIR_E : x = ~x + y + 9;   break;
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3416
		case DIR_SE: x = y;            break;
8808
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  3417
		case DIR_S : x = x + y - 7;    break;
b1b45098092f (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8801
diff changeset
  3418
		case DIR_W : x = ~y + x + 9;   break;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  3419
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3420
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3421
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3422
	/* do not reverse when approaching red signal */
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3423
	if (!signal && x + 4 >= TILE_SIZE) {
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3424
		/* we are too near the tile end, reverse now */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3425
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3426
		ReverseTrainDirection(v);
742
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3427
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3428
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3429
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  3430
	/* slow down */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3431
	v->vehstatus |= VS_TRAIN_SLOWING;
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3432
	uint16 break_speed = _breakdown_speeds[x & 0xF];
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
  3433
	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
  3434
60511ced2372 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3435
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3436
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3437
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3438
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3439
/**
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3440
 * Determines whether train would like to leave the tile
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3441
 * @param v train to test
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3442
 * @return true iff vehicle is NOT entering or inside a depot or tunnel/bridge
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3443
 */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3444
static bool TrainCanLeaveTile(const Vehicle *v)
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3445
{
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3446
	/* Exit if inside a tunnel/bridge or a depot */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3447
	if (v->u.rail.track == TRACK_BIT_WORMHOLE || v->u.rail.track == TRACK_BIT_DEPOT) return false;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3448
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3449
	TileIndex tile = v->tile;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3450
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3451
	/* entering a tunnel/bridge? */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3452
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3453
		DiagDirection dir = GetTunnelBridgeDirection(tile);
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3454
		if (DiagDirToDir(dir) == v->direction) return false;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3455
	}
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3456
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3457
	/* entering a depot? */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3458
	if (IsTileDepotType(tile, TRANSPORT_RAIL)) {
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3459
		DiagDirection dir = ReverseDiagDir(GetRailDepotDirection(tile));
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3460
		if (DiagDirToDir(dir) == v->direction) return false;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3461
	}
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3462
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3463
	return true;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3464
}
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3465
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3466
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3467
/**
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3468
 * Determines whether train is approaching a rail-road crossing
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3469
 *   (thus making it barred)
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3470
 * @param v front engine of train
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3471
 * @return TileIndex of crossing the train is approaching, else INVALID_TILE
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3472
 * @pre v in non-crashed front engine
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3473
 */
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3474
static TileIndex TrainApproachingCrossingTile(const Vehicle *v)
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3475
{
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3476
	assert(IsFrontEngine(v));
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3477
	assert(!(v->vehstatus & VS_CRASHED));
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3478
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3479
	if (!TrainCanLeaveTile(v)) return INVALID_TILE;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3480
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3481
	DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track);
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3482
	TileIndex tile = v->tile + TileOffsByDiagDir(dir);
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3483
9102
238c3c97c249 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz
parents: 9094
diff changeset
  3484
	/* not a crossing || wrong axis || unusable rail (wrong type or owner) */
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3485
	if (!IsLevelCrossingTile(tile) || DiagDirToAxis(dir) == GetCrossingRoadAxis(tile) ||
9102
238c3c97c249 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz
parents: 9094
diff changeset
  3486
			!CheckCompatibleRail(v, tile)) {
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3487
		return INVALID_TILE;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3488
	}
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3489
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3490
	return tile;
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3491
}
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3492
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3493
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3494
/**
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3495
 * Checks for line end. Also, bars crossing at next tile if needed
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3496
 *
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3497
 * @param v vehicle we are checking
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3498
 * @return true iff we did NOT have to reverse
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3499
 */
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3500
static bool TrainCheckIfLineEnds(Vehicle *v)
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3501
{
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3502
	/* First, handle broken down train */
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3503
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3504
	int t = v->breakdown_ctr;
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3505
	if (t > 1) {
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3506
		v->vehstatus |= VS_TRAIN_SLOWING;
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3507
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3508
		uint16 break_speed = _breakdown_speeds[GB(~t, 4, 4)];
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3509
		if (break_speed < v->cur_speed) v->cur_speed = break_speed;
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3510
	} else {
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3511
		v->vehstatus &= ~VS_TRAIN_SLOWING;
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3512
	}
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3513
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3514
	if (!TrainCanLeaveTile(v)) return true;
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3515
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3516
	/* Determine the non-diagonal direction in which we will exit this tile */
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3517
	DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track);
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3518
	/* Calculate next tile */
8830
4184b599c73d (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8813
diff changeset
  3519
	TileIndex tile = v->tile + TileOffsByDiagDir(dir);
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3520
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3521
	/* Determine the track status on the next tile */
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3522
	TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0, ReverseDiagDir(dir)) & _reachable_tracks[dir];
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3523
	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts);
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3524
	TrackdirBits red_signals = TrackStatusToRedSignals(ts);
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3525
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3526
	/* We are sure the train is not entering a depot, it is detected above */
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3527
8978
e8610e299942 (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz
parents: 8963
diff changeset
  3528
	/* mask unreachable track bits if we are forbidden to do 90deg turns */
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3529
	TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9049
diff changeset
  3530
	if (_patches.pathfinder_for_trains != VPF_NTP && _patches.forbid_90_deg) {
8978
e8610e299942 (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz
parents: 8963
diff changeset
  3531
		bits &= ~TrackCrossesTracks(FindFirstTrack(v->u.rail.track));
e8610e299942 (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz
parents: 8963
diff changeset
  3532
	}
e8610e299942 (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz
parents: 8963
diff changeset
  3533
9102
238c3c97c249 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz
parents: 9094
diff changeset
  3534
	/* no suitable trackbits at all || unusable rail (wrong type or owner) */
238c3c97c249 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz
parents: 9094
diff changeset
  3535
	if (bits == TRACK_BIT_NONE || !CheckCompatibleRail(v, tile)) {
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3536
		return TrainApproachingLineEnd(v, false);
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3537
	}
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3538
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3539
	/* approaching red signal */
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9110
diff changeset
  3540
	if ((trackdirbits & red_signals) != 0) return TrainApproachingLineEnd(v, true);
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3541
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3542
	/* approaching a rail/road crossing? then make it red */
8852
537ef841c5a4 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8846
diff changeset
  3543
	if (IsLevelCrossingTile(tile)) MaybeBarCrossingWithSound(tile);
8801
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3544
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3545
	return true;
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3546
}
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3547
a1eb885cc491 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8798
diff changeset
  3548
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3549
static void TrainLocoHandler(Vehicle *v, bool mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3550
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3551
	/* train has crashed? */
7972
dff055fff851 (svn r10983) -Codechange: use vehstatus & VS_CRASHED instead of some other "methods" custom to each vehicle to determine whether the vehicle is crashed.
rubidium
parents: 7921
diff changeset
  3552
	if (v->vehstatus & VS_CRASHED) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3553
		if (!mode) HandleCrashedTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3554
		return;
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
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
  3557
	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
  3558
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3559
	/* train is broken down? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3560
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3561
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3562
			HandleBrokenTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3563
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3564
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3565
		v->breakdown_ctr--;
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
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  3568
	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
  3569
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3570
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3571
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3572
	/* 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
  3573
	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
  3574
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3575
	if (ProcessTrainOrder(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3576
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3577
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3578
		v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3579
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3580
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3581
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3582
7090
8e4a4ac64b2d (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 7089
diff changeset
  3583
	v->HandleLoading(mode);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3584
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
  3585
	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
  3586
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
  3587
	if (CheckTrainStayInDepot(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3588
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3589
	if (!mode) HandleLocomotiveSmokeCloud(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3590
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3591
	int j = UpdateTrainSpeed(v);
8922
88bec308f864 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8886
diff changeset
  3592
88bec308f864 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8886
diff changeset
  3593
	/* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */
88bec308f864 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8886
diff changeset
  3594
	if (v->cur_speed == 0 && v->u.rail.last_speed == 0 && v->vehstatus & VS_STOPPED) {
88bec308f864 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8886
diff changeset
  3595
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
88bec308f864 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8886
diff changeset
  3596
	}
88bec308f864 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8886
diff changeset
  3597
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3598
	if (j == 0) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  3599
		/* 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
  3600
		if (v->cur_speed != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3601
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3602
		TrainCheckIfLineEnds(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3603
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3604
		do {
9206
12d35b56014c (svn r12384) -Fix (r2428): do not disconnect train when reversing, it may 'think' it is whole in a depot
smatz
parents: 9202
diff changeset
  3605
			TrainController(v, NULL, true);
1922
797081e56d13 (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  3606
			CheckTrainCollision(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3607
			if (v->cur_speed <= 0x100)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3608
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3609
		} while (--j != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3610
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3611
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3612
	SetLastSpeed(v, v->cur_speed);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3613
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3614
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3615
7984
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3616
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3617
Money Train::GetRunningCost() const
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3618
{
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3619
	Money cost = 0;
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3620
	const Vehicle *v = this;
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3621
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3622
	do {
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3623
		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3624
9118
bf19f7f901bc (svn r12205) -Codechange: rename RailVehicleInfo::running_cost_base to running_cost, inline with other vehicle types (It is the factor, not the base)
peter1138
parents: 9116
diff changeset
  3625
		byte cost_factor = GetVehicleProperty(v, 0x0D, rvi->running_cost);
7984
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3626
		if (cost_factor == 0) continue;
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3627
9122
bc3651767850 (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 9118
diff changeset
  3628
		cost += cost_factor * GetPriceByIndex(rvi->running_cost_class);
7984
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3629
	} while ((v = GetNextVehicle(v)) != NULL);
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3630
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3631
	return cost;
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3632
}
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3633
c539c9368e3c (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7982
diff changeset
  3634
7631
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3635
void Train::Tick()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3636
{
7631
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3637
	if (_age_cargo_skip_counter == 0) this->cargo.AgeCargo();
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3638
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3639
	this->tick_counter++;
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3640
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3641
	if (IsFrontEngine(this)) {
9052
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  3642
		if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
7631
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3643
		this->current_order_time++;
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3644
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3645
		TrainLocoHandler(this, false);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3646
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  3647
		/* make sure vehicle wasn't deleted. */
7631
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3648
		if (this->type == VEH_TRAIN && IsFrontEngine(this))
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3649
			TrainLocoHandler(this, true);
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  3650
	} else if (IsFreeWagon(this) && HASBITS(this->vehstatus, VS_CRASHED)) {
8192
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
  3651
		/* Delete flooded standalone wagon chain */
dcc489afe013 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 8191
diff changeset
  3652
		if (++this->u.rail.crash_anim_pos >= 4400) DeleteVehicleChain(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3653
	}
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
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  3656
#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
  3657
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3658
static void CheckIfTrainNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3659
{
7998
f1a308b61b8f (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium
parents: 7993
diff changeset
  3660
	if (_patches.servint_trains == 0 || !VehicleNeedsService(v)) return;
f1a308b61b8f (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium
parents: 7993
diff changeset
  3661
	if (v->IsInDepot()) {
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3662
		VehicleServiceInDepot(v);
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3663
		return;
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3664
	}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3665
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3666
	TrainFindDepotData 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
  3667
	/* 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
  3668
	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
  3669
		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
  3670
			/* 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
  3671
			 * 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
  3672
			 * schedule? */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3673
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3674
			v->current_order.flags = 0;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  3675
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
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
		return;
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
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3680
	const Depot* depot = GetDepotByTile(tfdd.tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3681
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3682
	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
  3683
			v->current_order.dest != depot->index &&
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8428
diff changeset
  3684
			!Chance16(3, 16)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3685
		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
  3686
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3687
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3688
	v->current_order.type = OT_GOTO_DEPOT;
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8796
diff changeset
  3689
	v->current_order.flags = OFB_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
  3690
	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
  3691
	v->dest_tile = tfdd.tile;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8840
diff changeset
  3692
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3693
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3694
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3695
void Train::OnNewDay()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3696
{
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3697
	if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3698
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3699
	if (IsFrontEngine(this)) {
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3700
		CheckVehicleBreakdown(this);
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3701
		AgeVehicle(this);
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3702
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3703
		CheckIfTrainNeedsService(this);
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3704
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3705
		CheckOrders(this);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3706
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3707
		/* update destination */
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3708
		if (this->current_order.type == OT_GOTO_STATION) {
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3709
			TileIndex tile = GetStation(this->current_order.dest)->train_tile;
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3710
			if (tile != 0) this->dest_tile = tile;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3711
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3712
9052
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  3713
		if (this->running_ticks != 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3714
			/* running costs */
9052
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  3715
			CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (364 * DAY_TICKS));
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  3716
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  3717
			this->profit_this_year -= cost.GetCost();
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  3718
			this->running_ticks = 0;
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3719
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3720
			SubtractMoneyFromPlayerFract(this->owner, cost);
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3721
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3722
			InvalidateWindow(WC_VEHICLE_DETAILS, this->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
  3723
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3724
		}
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3725
	} else if (IsTrainEngine(this)) {
7520
6544981295fa (svn r10288) -Fix [FS#202]: also age engines that aren't front-engines (based on a patch by kaan)
truelight
parents: 7519
diff changeset
  3726
		/* Also age engines that aren't front engines */
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8922
diff changeset
  3727
		AgeVehicle(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3728
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3729
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3730
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6502
diff changeset
  3731
void TrainsYearlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3732
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3733
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3734
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3735
	FOR_ALL_VEHICLES(v) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  3736
		if (v->type == VEH_TRAIN && IsFrontEngine(v)) {
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  3737
			/* show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list) */
9110
55864e1fc263 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz
parents: 9108
diff changeset
  3738
			if (_patches.train_income_warn && v->owner == _local_player && v->age >= 730 && v->GetDisplayProfitThisYear() < 0) {
55864e1fc263 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz
parents: 9108
diff changeset
  3739
				SetDParam(1, v->GetDisplayProfitThisYear());
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  3740
				SetDParam(0, v->unitnumber);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3741
				AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3742
					STR_TRAIN_IS_UNPROFITABLE,
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9208
diff changeset
  3743
					NM_SMALL, NF_VIEWPORT | NF_VEHICLE, NT_ADVICE, DNC_NONE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3744
					v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3745
					0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3746
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3747
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3748
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3749
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3750
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3751
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3752
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3753
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3754
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3755
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6502
diff changeset
  3756
void InitializeTrains()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3757
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3758
	_age_cargo_skip_counter = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3759
}
2855
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
 * 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
  3763
 * 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
  3764
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6502
diff changeset
  3765
void ConnectMultiheadedTrains()
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3766
{
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3767
	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
  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
	FOR_ALL_VEHICLES(v) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  3770
		if (v->type == VEH_TRAIN) {
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3771
			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
  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
	FOR_ALL_VEHICLES(v) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  3776
		if (v->type == VEH_TRAIN && IsFrontEngine(v)) {
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3777
			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
  3778
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3779
			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
  3780
				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
  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
				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
  3783
					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
  3784
						/* 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
  3785
						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
  3786
						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
  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
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3789
					Vehicle *w;
9191
ae14770c829a (svn r12368) -Codechange: use explicit body for loops and conditions and remove -Wno-empty-body from the configure script
smatz
parents: 9163
diff changeset
  3790
					for (w = u->Next(); w != NULL && (w->engine_type != u->engine_type || w->u.rail.other_multiheaded_part != NULL); w = GetNextVehicle(w)) {}
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3791
					if (w != NULL) {
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3792
						/* we found a car to partner with this engine. Now we will make sure it face the right way */
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3793
						if (IsTrainEngine(w)) {
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3794
							ClearTrainEngine(w);
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3795
							w->spritenum++;
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3796
						}
6476
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3797
						w->u.rail.other_multiheaded_part = u;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3798
						u->u.rail.other_multiheaded_part = w;
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3799
					} else {
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3800
						/* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */
5bd4843b681b (svn r8893) -Fix
tron
parents: 6467
diff changeset
  3801
						ClearMultiheaded(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
  3802
					}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3803
				}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3804
			} 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
  3805
		}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3806
	}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3807
}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3808
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6903
diff changeset
  3809
/**
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3810
 *  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
  3811
 *  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
  3812
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6502
diff changeset
  3813
void ConvertOldMultiheadToNew()
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3814
{
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3815
	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
  3816
	FOR_ALL_VEHICLES(v) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  3817
		if (v->type == VEH_TRAIN) {
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
  3818
			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
  3819
		}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3820
	}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3821
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3822
	FOR_ALL_VEHICLES(v) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  3823
		if (v->type == VEH_TRAIN) {
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8419
diff changeset
  3824
			if (HasBit(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) {
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3825
				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
  3826
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
				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
  3828
					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
  3829
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  3830
					ClrBit(u->subtype, 7);
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
  3831
					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
  3832
						case 0: /* TS_Front_Engine */
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
  3833
							if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(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
  3834
							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
  3835
							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
  3836
							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
  3837
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
  3838
						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
  3839
							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
  3840
							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
  3841
							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
  3842
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
  3843
						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
  3844
							u->subtype = 0;
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
  3845
							if (rvi->railveh_type == RAILVEH_WAGON) {
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
  3846
								// 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
  3847
								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
  3848
								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
  3849
							}
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
  3850
							if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
2855
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3851
								// 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
  3852
								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
  3853
								break;
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3854
							}
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6109
diff changeset
  3855
							if (rvi->railveh_type == RAILVEH_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
  3856
							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
  3857
							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
  3858
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
  3859
						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
  3860
							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
  3861
							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
  3862
							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
  3863
							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
  3864
						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
  3865
					}
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
  3866
				} 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
  3867
			}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3868
		}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3869
	}
950b5a56f9d5 (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3870
}