src/train_cmd.cpp
author frosch
Thu, 13 Nov 2008 20:26:06 +0000
changeset 10328 7500e2e4c8ab
parent 10314 ac0958307f68
child 10343 c9a124b457d3
permissions -rw-r--r--
(svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9070
diff changeset
     3
/** @file train_cmd.cpp Handling of trains. */
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1883
diff changeset
     6
#include "openttd.h"
3234
a2791a480b71 (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
bda6f85eefaa (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"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
     9
#include "tile_cmd.h"
6453
226bcddeba32 (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: 6422
diff changeset
    10
#include "landscape.h"
2561
233dafd7cf6c (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
c9d989783c55 (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
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
    13
#include "tunnel_map.h"
6772
8672ba1bebc9 (svn r10008) -Codechange: Move a couple of functions related to articulated vehicles to a file of their own.
maedhros
parents: 6771
diff changeset
    14
#include "articulated_vehicles.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    15
#include "command_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "pathfind.h"
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    17
#include "npf.h"
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8784
diff changeset
    18
#include "station_base.h"
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8712
diff changeset
    19
#include "news_func.h"
8786
1823ff88a054 (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: 8785
diff changeset
    20
#include "engine_func.h"
9070
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9041
diff changeset
    21
#include "engine_base.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    22
#include "company_func.h"
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    23
#include "company_base.h"
8962
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8961
diff changeset
    24
#include "depot_base.h"
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8961
diff changeset
    25
#include "depot_func.h"
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1531
diff changeset
    26
#include "waypoint.h"
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1698
diff changeset
    27
#include "vehicle_gui.h"
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
    28
#include "train.h"
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
    29
#include "bridge.h"
2982
eb7f50dcb249 (svn r3557) - NewGRF: move callback enums to a new file as more than just engines will use them
peter1138
parents: 2970
diff changeset
    30
#include "newgrf_callbacks.h"
2962
f0a49b646c48 (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
    31
#include "newgrf_engine.h"
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
    32
#include "newgrf_sound.h"
3727
3a3ebb0bde7e (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
    33
#include "newgrf_text.h"
8100
6bc08f98ec16 (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: 8088
diff changeset
    34
#include "direction_func.h"
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
    35
#include "yapf/yapf.h"
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
    36
#include "yapf/follow_track.hpp"
6091
c8827d9ae04a (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: 5998
diff changeset
    37
#include "cargotype.h"
6643
f81bee57bc09 (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: 6608
diff changeset
    38
#include "group.h"
7802
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7783
diff changeset
    39
#include "table/sprites.h"
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8081
diff changeset
    40
#include "tunnelbridge_map.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8108
diff changeset
    41
#include "strings_func.h"
8131
160939e24ed3 (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: 8119
diff changeset
    42
#include "functions.h"
160939e24ed3 (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: 8119
diff changeset
    43
#include "window_func.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8139
diff changeset
    44
#include "date_func.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8143
diff changeset
    45
#include "vehicle_func.h"
8157
1505def01945 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8145
diff changeset
    46
#include "sound_func.h"
8238
d47a86c79603 (svn r11802) -Fix [FS#716]: do not crash trains when leaving depot to a very long track
smatz
parents: 8236
diff changeset
    47
#include "signal_func.h"
8211
29a8510dfd62 (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8197
diff changeset
    48
#include "variables.h"
8212
cf3fce5c7464 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8211
diff changeset
    49
#include "autoreplace_gui.h"
8224
c5a64d87cc54 (svn r11787) -Codechange: more header rewrites. This time related to viewport.h.
rubidium
parents: 8212
diff changeset
    50
#include "gfx_func.h"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8267
diff changeset
    51
#include "settings_type.h"
8784
c2e9d649a9ce (svn r12488) -Codechange: split order.h into order_base.h and order_func.h.
rubidium
parents: 8781
diff changeset
    52
#include "order_func.h"
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 9000
diff changeset
    53
#include "newgrf_station.h"
9009
6684576ef32b (svn r12804) -Codechange: move the effect vehicle handling out of vehicle.cpp
rubidium
parents: 9008
diff changeset
    54
#include "effectvehicle_func.h"
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
    55
#include "gamelog.h"
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
    56
#include "network/network.h"
9809
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
    57
#include "pbs.h"
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8081
diff changeset
    58
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    59
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    60
#include "table/train_cmd.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
    62
static Track ChooseTrainTrack(Vehicle* v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck);
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
    63
static bool TrainCheckIfLineEnds(Vehicle *v);
8710
cb7cf95986d9 (svn r12384) -Fix (r2428): do not disconnect train when reversing, it may 'think' it is whole in a depot
smatz
parents: 8706
diff changeset
    64
static void TrainController(Vehicle *v, Vehicle *nomove, bool update_image);
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
    65
static TileIndex TrainApproachingCrossingTile(const Vehicle *v);
9822
ab2bd3cf424f (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium
parents: 9821
diff changeset
    66
static void CheckIfTrainNeedsService(Vehicle *v);
9819
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
    67
static void CheckNextTrainTile(Vehicle *v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
4344
7e123fec5b0b (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
    69
static const byte _vehicle_initial_x_fract[4] = {10, 8, 4,  8};
7e123fec5b0b (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
    70
static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
8248
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    71
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    72
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    73
/**
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    74
 * Determine the side in which the train will leave the tile
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    75
 *
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    76
 * @param direction vehicle direction
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    77
 * @param track vehicle track bits
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    78
 * @return side of tile the train will leave
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    79
 */
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    80
static inline DiagDirection TrainExitDir(Direction direction, TrackBits track)
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    81
{
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    82
	static const TrackBits state_dir_table[DIAGDIR_END] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER };
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    83
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    84
	DiagDirection diagdir = DirToDiagDir(direction);
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    85
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    86
	/* Determine the diagonal direction in which we will exit this tile */
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    87
	if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) {
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    88
		diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    89
	}
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    90
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    91
	return diagdir;
2174c426ac5a (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz
parents: 8238
diff changeset
    92
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
5163
459b243f8413 (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
459b243f8413 (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
/** Return the cargo weight multiplier to use for a rail vehicle
5316
27fa85736f8a (svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents: 5303
diff changeset
    96
 * @param cargo Cargo type to get multiplier for
5163
459b243f8413 (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
    97
 * @return Cargo weight multiplier
459b243f8413 (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
    98
 */
5316
27fa85736f8a (svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents: 5303
diff changeset
    99
byte FreightWagonMult(CargoID cargo)
5163
459b243f8413 (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
   100
{
6114
f15b4f5533b2 (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: 6091
diff changeset
   101
	if (!GetCargo(cargo)->is_freight) return 1;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   102
	return _settings_game.vehicle.freight_trains;
5163
459b243f8413 (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
   103
}
459b243f8413 (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
   104
459b243f8413 (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
   105
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   106
/**
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   107
 * Recalculates the cached total power of a train. Should be called when the consist is changed
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   108
 * @param v First vehicle of the consist.
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   109
 */
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   110
void TrainPowerChanged(Vehicle *v)
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   111
{
6490
fe4443fba5e3 (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: 6484
diff changeset
   112
	uint32 total_power = 0;
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   113
	uint32 max_te = 0;
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   114
7492
35acee076719 (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: 7488
diff changeset
   115
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
6172
e6d7a5b3d63f (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: 6155
diff changeset
   116
		RailType railtype = GetRailType(u->tile);
9738
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   117
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   118
		/* Power is not added for articulated parts */
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   119
		if (!IsArticulatedPart(u)) {
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   120
			bool engine_has_power = HasPowerOnRail(u->u.rail.railtype, railtype);
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   121
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   122
			const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   123
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   124
			if (engine_has_power) {
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   125
				uint16 power = GetVehicleProperty(u, 0x0B, rvi_u->power);
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   126
				if (power != 0) {
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   127
					/* Halve power for multiheaded parts */
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   128
					if (IsMultiheaded(u)) power /= 2;
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   129
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   130
					total_power += power;
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   131
					/* Tractive effort in (tonnes * 1000 * 10 =) N */
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   132
					max_te += (u->u.rail.cached_veh_weight * 10000 * GetVehicleProperty(u, 0x1F, rvi_u->tractive_effort)) / 256;
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   133
				}
6490
fe4443fba5e3 (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: 6484
diff changeset
   134
			}
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   135
		}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   136
9738
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   137
		if (HasBit(u->u.rail.flags, VRF_POWEREDWAGON) && HasPowerOnRail(v->u.rail.railtype, railtype)) {
6490
fe4443fba5e3 (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: 6484
diff changeset
   138
			total_power += RailVehInfo(u->u.rail.first_engine)->pow_wag_power;
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   139
		}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   140
	}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   141
6490
fe4443fba5e3 (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: 6484
diff changeset
   142
	if (v->u.rail.cached_power != total_power || v->u.rail.cached_max_te != max_te) {
7539
5f41050498bd (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: 7529
diff changeset
   143
		/* If it has no power (no catenary), stop the train */
5f41050498bd (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: 7529
diff changeset
   144
		if (total_power == 0) v->vehstatus |= VS_STOPPED;
5f41050498bd (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: 7529
diff changeset
   145
6490
fe4443fba5e3 (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: 6484
diff changeset
   146
		v->u.rail.cached_power = total_power;
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   147
		v->u.rail.cached_max_te = max_te;
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   148
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8344
diff changeset
   149
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   150
	}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   151
}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   152
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   153
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   154
/**
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   155
 * Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   156
 * the consist changes.
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   157
 * @param v First vehicle of the consist.
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   158
 */
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   159
static void TrainCargoChanged(Vehicle *v)
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   160
{
5162
f8da34a35a59 (svn r7268) -Codechange: change rail vehicle cached weight from 16 to 32 bit values to avoid possible overflows.
peter1138
parents: 5116
diff changeset
   161
	uint32 weight = 0;
1905
c53f8d71f9b6 (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
7492
35acee076719 (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: 7488
diff changeset
   163
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
7010
6f0d9f03180d (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: 7002
diff changeset
   164
		uint32 vweight = GetCargo(u->cargo_type)->weight * u->cargo.Count() * FreightWagonMult(u->cargo_type) / 16;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   165
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   166
		/* Vehicle weight is not added for articulated parts. */
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   167
		if (!IsArticulatedPart(u)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   168
			/* vehicle weight is the sum of the weight of the vehicle and the weight of its cargo */
6572
f1f1a685a479 (svn r9780) -Codechange: [NewGRF] Add support for variable vehicle weight for trains (callback 36, property 16)
peter1138
parents: 6565
diff changeset
   169
			vweight += GetVehicleProperty(u, 0x16, RailVehInfo(u->engine_type)->weight);
9738
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   170
		}
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   171
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   172
		/* powered wagons have extra weight added */
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   173
		if (HasBit(u->u.rail.flags, VRF_POWEREDWAGON)) {
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   174
			vweight += RailVehInfo(u->u.rail.first_engine)->pow_wag_weight;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   175
		}
1905
c53f8d71f9b6 (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
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   177
		/* consist weight is the sum of the weight of all vehicles in the consist */
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   178
		weight += vweight;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   179
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   180
		/* store vehicle weight in cache */
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   181
		u->u.rail.cached_veh_weight = vweight;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   182
	}
1905
c53f8d71f9b6 (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
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   184
	/* store consist weight in cache */
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   185
	v->u.rail.cached_weight = weight;
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   186
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   187
	/* Now update train power (tractive effort is dependent on weight) */
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   188
	TrainPowerChanged(v);
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   189
}
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   190
3355
e414a0b104a6 (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
   191
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   192
/** Logs a bug in GRF and shows a warning message if this
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   193
 * is for the first time this happened.
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   194
 * @param u first vehicle of chain
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   195
 */
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   196
static void RailVehicleLengthChanged(const Vehicle *u)
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   197
{
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   198
	/* show a warning once for each engine in whole game and once for each GRF after each game load */
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   199
	const Engine *engine = GetEngine(u->engine_type);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   200
	uint32 grfid = engine->grffile->grfid;
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   201
	GRFConfig *grfconfig = GetGRFConfig(grfid);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   202
	if (GamelogGRFBugReverse(grfid, engine->internal_id) || !HasBit(grfconfig->grf_bugs, GBUG_VEH_LENGTH)) {
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   203
		SetBit(grfconfig->grf_bugs, GBUG_VEH_LENGTH);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   204
		SetDParamStr(0, grfconfig->name);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   205
		SetDParam(1, u->engine_type);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   206
		ShowErrorMessage(STR_NEWGRF_BROKEN_VEHICLE_LENGTH, STR_NEWGRF_BROKEN, 0, 0);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   207
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   208
		/* debug output */
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   209
		char buffer[512];
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   210
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   211
		SetDParamStr(0, grfconfig->name);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   212
		GetString(buffer, STR_NEWGRF_BROKEN, lastof(buffer));
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   213
		DEBUG(grf, 0, "%s", buffer + 3);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   214
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   215
		SetDParam(1, u->engine_type);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   216
		GetString(buffer, STR_NEWGRF_BROKEN_VEHICLE_LENGTH, lastof(buffer));
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   217
		DEBUG(grf, 0, "%s", buffer + 3);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   218
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   219
		if (!_networking) _pause_game = -1;
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   220
	}
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   221
}
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   222
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   223
/** Checks if lengths of all rail vehicles are valid. If not, shows an error message. */
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   224
void CheckTrainsLengths()
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   225
{
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   226
	const Vehicle *v;
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   227
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   228
	FOR_ALL_VEHICLES(v) {
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   229
		if (v->type == VEH_TRAIN && v->First() == v && !(v->vehstatus & VS_CRASHED)) {
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   230
			for (const Vehicle *u = v, *w = v->Next(); w != NULL; u = w, w = w->Next()) {
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   231
				if (u->u.rail.track != TRACK_BIT_DEPOT) {
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   232
					if ((w->u.rail.track != TRACK_BIT_DEPOT &&
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   233
							max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->u.rail.cached_veh_length) ||
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   234
							(w->u.rail.track == TRACK_BIT_DEPOT && TicksToLeaveDepot(u) <= 0)) {
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   235
						SetDParam(0, v->index);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   236
						SetDParam(1, v->owner);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   237
						ShowErrorMessage(INVALID_STRING_ID, STR_BROKEN_VEHICLE_LENGTH, 0, 0);
9705
39330e3f52c2 (svn r13817) -Cleanup (r13816): no need to check for ENABLE_NETWORK, _networking is defined anyway
smatz
parents: 9704
diff changeset
   238
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   239
						if (!_networking) _pause_game = -1;
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   240
					}
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   241
				}
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   242
			}
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   243
		}
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   244
	}
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   245
}
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   246
3355
e414a0b104a6 (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
   247
/**
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   248
 * Recalculates the cached stuff of a train. Should be called each time a vehicle is added
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   249
 * to/removed from the chain, and when the game is loaded.
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   250
 * Note: this needs to be called too for 'wagon chains' (in the depot, without an engine)
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   251
 * @param v First vehicle of the chain.
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   252
 * @param same_length should length of vehicles stay the same?
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   253
 */
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   254
void TrainConsistChanged(Vehicle *v, bool same_length)
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   255
{
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   256
	uint16 max_speed = UINT16_MAX;
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   257
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   258
	assert(v->type == VEH_TRAIN);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   259
	assert(IsFrontEngine(v) || IsFreeWagon(v));
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   260
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   261
	const RailVehicleInfo *rvi_v = RailVehInfo(v->engine_type);
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   262
	EngineID first_engine = IsFrontEngine(v) ? v->engine_type : INVALID_ENGINE;
2587
31647a23da0b (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
   263
	v->u.rail.cached_total_length = 0;
8236
8a5dd0b42e47 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8230
diff changeset
   264
	v->u.rail.compatible_railtypes = RAILTYPES_NONE;
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   265
8178
28e437261743 (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: 8175
diff changeset
   266
	bool train_can_tilt = true;
28e437261743 (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: 8175
diff changeset
   267
7492
35acee076719 (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: 7488
diff changeset
   268
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   269
		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   270
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   271
		/* Check the v->first cache. */
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   272
		assert(u->First() == v);
2993
5fa94545a407 (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
   273
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   274
		/* update the 'first engine' */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   275
		u->u.rail.first_engine = v == u ? INVALID_ENGINE : first_engine;
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
   276
		u->u.rail.railtype = rvi_u->railtype;
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   277
9515
ff5c2a07215b (svn r13507) -Fix (r12856): first engine change should've been moved too
peter1138
parents: 9488
diff changeset
   278
		if (IsTrainEngine(u)) first_engine = u->engine_type;
ff5c2a07215b (svn r13507) -Fix (r12856): first engine change should've been moved too
peter1138
parents: 9488
diff changeset
   279
9037
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   280
		/* Set user defined data to its default value */
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   281
		u->u.rail.user_def_data = rvi_u->user_def_data;
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   282
	}
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   283
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   284
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   285
		/* Update user defined data (must be done before other properties) */
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   286
		u->u.rail.user_def_data = GetVehicleProperty(u, 0x25, u->u.rail.user_def_data);
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   287
	}
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   288
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   289
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   290
		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   291
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   292
		if (!HasBit(EngInfo(u->engine_type)->misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
28b55f76daa5 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138
parents: 9022
diff changeset
   293
6603
076b05312635 (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: 6594
diff changeset
   294
		/* Cache wagon override sprite group. NULL is returned if there is none */
076b05312635 (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: 6594
diff changeset
   295
		u->u.rail.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->u.rail.first_engine);
076b05312635 (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: 6594
diff changeset
   296
7802
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7783
diff changeset
   297
		/* Reset color map */
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7783
diff changeset
   298
		u->colormap = PAL_NONE;
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7783
diff changeset
   299
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   300
		if (rvi_u->visual_effect != 0) {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   301
			u->u.rail.cached_vis_effect = rvi_u->visual_effect;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   302
		} else {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   303
			if (IsTrainWagon(u) || IsArticulatedPart(u)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   304
				/* Wagons and articulated parts have no effect by default */
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   305
				u->u.rail.cached_vis_effect = 0x40;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   306
			} else if (rvi_u->engclass == 0) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   307
				/* Steam is offset by -4 units */
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   308
				u->u.rail.cached_vis_effect = 4;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   309
			} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   310
				/* Diesel fumes and sparks come from the centre */
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   311
				u->u.rail.cached_vis_effect = 8;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   312
			}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   313
		}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   314
9738
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   315
		/* Check powered wagon / visual effect callback */
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   316
		if (HasBit(EngInfo(u->engine_type)->callbackmask, CBM_TRAIN_WAGON_POWER)) {
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   317
			uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, u->engine_type, u);
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   318
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   319
			if (callback != CALLBACK_FAILED) u->u.rail.cached_vis_effect = GB(callback, 0, 8);
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   320
		}
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   321
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   322
		if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON &&
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   323
			UsesWagonOverride(u) && !HasBit(u->u.rail.cached_vis_effect, 7)) {
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   324
			/* wagon is powered */
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   325
			SetBit(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   326
		} else {
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   327
			ClrBit(u->u.rail.flags, VRF_POWEREDWAGON);
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   328
		}
404c804ac68a (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138
parents: 9705
diff changeset
   329
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   330
		if (!IsArticulatedPart(u)) {
3355
e414a0b104a6 (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
   331
			/* Do not count powered wagons for the compatible railtypes, as wagons always
e414a0b104a6 (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
   332
			   have railtype normal */
e414a0b104a6 (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
   333
			if (rvi_u->power > 0) {
e414a0b104a6 (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
   334
				v->u.rail.compatible_railtypes |= GetRailTypeInfo(u->u.rail.railtype)->powered_railtypes;
e414a0b104a6 (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
   335
			}
e414a0b104a6 (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
   336
5116
3c0c3da214ca (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   337
			/* Some electric engines can be allowed to run on normal rail. It happens to all
3c0c3da214ca (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   338
			 * existing electric engines when elrails are disabled and then re-enabled */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   339
			if (HasBit(u->u.rail.flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL)) {
5116
3c0c3da214ca (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   340
				u->u.rail.railtype = RAILTYPE_RAIL;
8236
8a5dd0b42e47 (svn r11800) -Codechange: move some functions to a more logical location + some type safety.
rubidium
parents: 8230
diff changeset
   341
				u->u.rail.compatible_railtypes |= RAILTYPES_RAIL;
5116
3c0c3da214ca (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   342
			}
3c0c3da214ca (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   343
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   344
			/* max speed is the minimum of the speed limits of all vehicles in the consist */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   345
			if ((rvi_u->railveh_type != RAILVEH_WAGON || _settings_game.vehicle.wagon_speed_limits) && !UsesWagonOverride(u)) {
6490
fe4443fba5e3 (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: 6484
diff changeset
   346
				uint16 speed = GetVehicleProperty(u, 0x09, rvi_u->max_speed);
fe4443fba5e3 (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: 6484
diff changeset
   347
				if (speed != 0) max_speed = min(speed, max_speed);
fe4443fba5e3 (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: 6484
diff changeset
   348
			}
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   349
		}
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   350
6644
327f66bc01c6 (svn r9875) -Fix (r9828): Only set carriage capacity if the wagon has not been refitted.
peter1138
parents: 6643
diff changeset
   351
		if (u->cargo_type == rvi_u->cargo_type && u->cargo_subtype == 0) {
327f66bc01c6 (svn r9875) -Fix (r9828): Only set carriage capacity if the wagon has not been refitted.
peter1138
parents: 6643
diff changeset
   352
			/* Set cargo capacity if we've not been refitted */
327f66bc01c6 (svn r9875) -Fix (r9828): Only set carriage capacity if the wagon has not been refitted.
peter1138
parents: 6643
diff changeset
   353
			u->cargo_cap = GetVehicleProperty(u, 0x14, rvi_u->capacity);
327f66bc01c6 (svn r9875) -Fix (r9828): Only set carriage capacity if the wagon has not been refitted.
peter1138
parents: 6643
diff changeset
   354
		}
6608
dc0b009642a4 (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: 6603
diff changeset
   355
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   356
		/* check the vehicle length (callback) */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   357
		uint16 veh_len = CALLBACK_FAILED;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   358
		if (HasBit(EngInfo(u->engine_type)->callbackmask, CBM_VEHICLE_LENGTH)) {
7215
7f90af37655d (svn r10493) -Codechange: update some callback ID enums to reflect their changed usage, add a few and update the comments.
rubidium
parents: 7196
diff changeset
   359
			veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, u);
3956
92362ba224b7 (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
   360
		}
92362ba224b7 (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
   361
		if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   362
		veh_len = 8 - 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
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   363
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   364
		/* verify length hasn't changed */
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   365
		if (same_length && veh_len != u->u.rail.cached_veh_length) RailVehicleLengthChanged(u);
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   366
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   367
		/* update vehicle length? */
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   368
		if (!same_length) u->u.rail.cached_veh_length = veh_len;
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   369
2587
31647a23da0b (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
   370
		v->u.rail.cached_total_length += u->u.rail.cached_veh_length;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   371
	}
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   372
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   373
	/* store consist weight/max speed in cache */
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   374
	v->u.rail.cached_max_speed = max_speed;
8178
28e437261743 (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: 8175
diff changeset
   375
	v->u.rail.cached_tilt = train_can_tilt;
3355
e414a0b104a6 (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
   376
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   377
	/* 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
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   378
	TrainCargoChanged(v);
8704
d0a22c1b20d7 (svn r12378) -Fix: refresh vehicle details window when cached values are updated
glx
parents: 8703
diff changeset
   379
8706
95102fcf66b4 (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: 8705
diff changeset
   380
	if (IsFrontEngine(v)) {
95102fcf66b4 (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: 8705
diff changeset
   381
		UpdateTrainAcceleration(v);
95102fcf66b4 (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: 8705
diff changeset
   382
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
95102fcf66b4 (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: 8705
diff changeset
   383
	}
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   384
}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   385
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   386
enum AccelType {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   387
	AM_ACCEL,
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   388
	AM_BRAKE
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   389
};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   390
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   391
/** new acceleration*/
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   392
static int GetTrainAcceleration(Vehicle *v, bool mode)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   393
{
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   394
	static const int absolute_max_speed = UINT16_MAX;
8169
31d57d9de945 (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: 8168
diff changeset
   395
	int max_speed = absolute_max_speed;
7561
5c350b238c0e (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: 7551
diff changeset
   396
	int speed = v->cur_speed * 10 / 16; // km-ish/h -> mp/h
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   397
	int curvecount[2] = {0, 0};
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   398
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   399
	/*first find the curve speed limit */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   400
	int numcurve = 0;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   401
	int sum = 0;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   402
	int pos = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   403
	int lastpos = -1;
7492
35acee076719 (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: 7488
diff changeset
   404
	for (const Vehicle *u = v; u->Next() != NULL; u = u->Next(), pos++) {
8175
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   405
		Direction this_dir = u->direction;
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   406
		Direction next_dir = u->Next()->direction;
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   407
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   408
		DirDiff dirdiff = DirDifference(this_dir, next_dir);
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   409
		if (dirdiff == DIRDIFF_SAME) continue;
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   410
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   411
		if (dirdiff == DIRDIFF_45LEFT) curvecount[0]++;
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   412
		if (dirdiff == DIRDIFF_45RIGHT) curvecount[1]++;
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   413
		if (dirdiff == DIRDIFF_45LEFT || dirdiff == DIRDIFF_45RIGHT) {
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   414
			if (lastpos != -1) {
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   415
				numcurve++;
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   416
				sum += pos - lastpos;
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   417
				if (pos - lastpos == 1) {
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   418
					max_speed = 88;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   419
				}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   420
			}
8175
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   421
			lastpos = pos;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   422
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   423
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   424
		/*if we have a 90 degree turn, fix the speed limit to 60 */
8175
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   425
		if (dirdiff == DIRDIFF_90LEFT || dirdiff == DIRDIFF_90RIGHT) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   426
			max_speed = 61;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   427
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   428
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   429
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   430
	if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   431
		int total = curvecount[0] + curvecount[1];
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   432
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   433
		if (curvecount[0] == 1 && curvecount[1] == 1) {
8169
31d57d9de945 (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: 8168
diff changeset
   434
			max_speed = absolute_max_speed;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   435
		} else if (total > 1) {
8175
4d10031a5495 (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138
parents: 8170
diff changeset
   436
			if (numcurve > 0) sum /= numcurve;
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7881
diff changeset
   437
			max_speed = 232 - (13 - Clamp(sum, 1, 12)) * (13 - Clamp(sum, 1, 12));
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   438
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   439
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   440
8169
31d57d9de945 (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: 8168
diff changeset
   441
	if (max_speed != absolute_max_speed) {
31d57d9de945 (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: 8168
diff changeset
   442
		/* Apply the engine's rail type curve speed advantage, if it slowed by curves */
31d57d9de945 (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: 8168
diff changeset
   443
		const RailtypeInfo *rti = GetRailTypeInfo(v->u.rail.railtype);
31d57d9de945 (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: 8168
diff changeset
   444
		max_speed += (max_speed / 2) * rti->curve_speed;
8178
28e437261743 (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: 8175
diff changeset
   445
28e437261743 (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: 8175
diff changeset
   446
		if (v->u.rail.cached_tilt) {
28e437261743 (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: 8175
diff changeset
   447
			/* Apply max_speed bonus of 20% for a tilting train */
28e437261743 (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: 8175
diff changeset
   448
			max_speed += max_speed / 5;
28e437261743 (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: 8175
diff changeset
   449
		}
8169
31d57d9de945 (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: 8168
diff changeset
   450
	}
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   451
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   452
	if (IsTileType(v->tile, MP_STATION) && IsFrontEngine(v)) {
8832
6d8cc04d39a4 (svn r12580) -Codechange: merge some logical related to non-stop orders.
rubidium
parents: 8830
diff changeset
   453
		if (v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile))) {
5998
2bfbade143ac (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: 5994
diff changeset
   454
			int station_length = GetStationByTile(v->tile)->GetPlatformLength(v->tile, DirToDiagDir(v->direction));
8170
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   455
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   456
			int st_max_speed = 120;
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   457
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   458
			int delta_v = v->cur_speed / (station_length + 1);
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   459
			if (v->max_speed > (v->cur_speed - delta_v)) {
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   460
				st_max_speed = v->cur_speed - (delta_v / 10);
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   461
			}
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   462
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   463
			st_max_speed = max(st_max_speed, 25 * station_length);
bf5a20b12ad8 (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138
parents: 8169
diff changeset
   464
			max_speed = min(max_speed, st_max_speed);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   465
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   466
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   467
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   468
	int mass = v->u.rail.cached_weight;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   469
	int power = v->u.rail.cached_power * 746;
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   470
	max_speed = min(max_speed, v->u.rail.cached_max_speed);
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   471
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   472
	int num = 0; //number of vehicles, change this into the number of axles later
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   473
	int incl = 0;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   474
	int drag_coeff = 20; //[1e-4]
7492
35acee076719 (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: 7488
diff changeset
   475
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   476
		num++;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   477
		drag_coeff += 3;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   478
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   479
		if (u->u.rail.track == TRACK_BIT_DEPOT) max_speed = min(max_speed, 61);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   480
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   481
		if (HasBit(u->u.rail.flags, VRF_GOINGUP)) {
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
   482
			incl += u->u.rail.cached_veh_weight * 60; //3% slope, quite a bit actually
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   483
		} else if (HasBit(u->u.rail.flags, VRF_GOINGDOWN)) {
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   484
			incl -= u->u.rail.cached_veh_weight * 60;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   485
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   486
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   487
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   488
	v->max_speed = max_speed;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   489
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   490
	const int area = 120;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   491
	const int friction = 35; //[1e-3]
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   492
	int resistance;
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   493
	if (v->u.rail.railtype != RAILTYPE_MAGLEV) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   494
		resistance = 13 * mass / 10;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   495
		resistance += 60 * num;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   496
		resistance += friction * mass * speed / 1000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   497
		resistance += (area * drag_coeff * speed * speed) / 10000;
3017
a75caf4efa2d (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
   498
	} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   499
		resistance = (area * (drag_coeff / 2) * speed * speed) / 10000;
3017
a75caf4efa2d (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
   500
	}
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   501
	resistance += incl;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   502
	resistance *= 4; //[N]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   503
5277
b60d0b6807d1 (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
   504
	/* Due to the mph to m/s conversion below, at speeds below 3 mph the force is
b60d0b6807d1 (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
   505
	 * actually double the train's power */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   506
	const int max_te = v->u.rail.cached_max_te; // [N]
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   507
	int force;
5277
b60d0b6807d1 (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
   508
	if (speed > 2) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   509
		switch (v->u.rail.railtype) {
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   510
			case RAILTYPE_RAIL:
3355
e414a0b104a6 (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
   511
			case RAILTYPE_ELECTRIC:
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   512
			case RAILTYPE_MONO:
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   513
				force = power / speed; //[N]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   514
				force *= 22;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   515
				force /= 10;
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   516
				if (mode == AM_ACCEL && force > max_te) force = max_te;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   517
				break;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   518
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   519
			default: NOT_REACHED();
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   520
			case RAILTYPE_MAGLEV:
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   521
				force = power / 25;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   522
				break;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   523
		}
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   524
	} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   525
		/* "kickoff" acceleration */
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   526
		force = (mode == AM_ACCEL && v->u.rail.railtype != RAILTYPE_MAGLEV) ? min(max_te, power) : power;
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   527
		force = max(force, (mass * 8) + resistance);
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   528
	}
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   529
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   530
	if (force <= 0) force = 10000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   531
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   532
	if (v->u.rail.railtype != RAILTYPE_MAGLEV) force = min(force, mass * 10 * 200);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   533
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   534
	if (mode == AM_ACCEL) {
1684
3b2df64cc706 (svn r2188) Revert r2185, because trains always accerlated (Requested by Celestar)
tron
parents: 1683
diff changeset
   535
		return (force - resistance) / (mass * 4);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   536
	} else {
5279
295d8b87b394 (svn r7425) -Fix (r1681): With realistic acceleration, guarantee a minimum braking
peter1138
parents: 5277
diff changeset
   537
		return min((-force - resistance) / (mass * 4), -10000 / (mass * 4));
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   538
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   539
}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   540
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   541
void UpdateTrainAcceleration(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
{
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   543
	assert(IsFrontEngine(v));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   545
	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
   546
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   547
	uint power = v->u.rail.cached_power;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   548
	uint weight = v->u.rail.cached_weight;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
	assert(weight != 0);
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7881
diff changeset
   550
	v->acceleration = Clamp(power / weight * 4, 1, 255);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   553
SpriteID Train::GetImage(Direction direction) const
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
{
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   555
	uint8 spritenum = this->spritenum;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   556
	SpriteID sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   558
	if (HasBit(this->u.rail.flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
   559
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   560
	if (is_custom_sprite(spritenum)) {
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   561
		sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   562
		if (sprite != 0) return sprite;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   563
9070
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9041
diff changeset
   564
		spritenum = GetEngine(this->engine_type)->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   566
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   567
	sprite = _engine_sprite_base[spritenum] + ((direction + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   568
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   569
	if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[spritenum];
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   570
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   571
	return sprite;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   572
}
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   573
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   574
static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &y)
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   575
{
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   576
	Direction dir = rear_head ? DIR_E : DIR_W;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   577
	uint8 spritenum = RailVehInfo(engine)->image_index;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   578
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   579
	if (is_custom_sprite(spritenum)) {
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   580
		SpriteID sprite = GetCustomVehicleIcon(engine, dir);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   581
		if (sprite != 0) {
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   582
			y += _traininfo_vehicle_pitch; // TODO Make this per-GRF
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   583
			return sprite;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   584
		}
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   585
9070
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9041
diff changeset
   586
		spritenum = GetEngine(engine)->image_index;
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   587
	}
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   588
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   589
	if (rear_head) spritenum++;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   590
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   591
	return ((6 + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]) + _engine_sprite_base[spritenum];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   594
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
   595
{
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   596
	if (RailVehInfo(engine)->railveh_type == RAILVEH_MULTIHEAD) {
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   597
		int yf = y;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   598
		int yr = y;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   599
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   600
		SpriteID spritef = GetRailIcon(engine, false, yf);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   601
		SpriteID spriter = GetRailIcon(engine, true, yr);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   602
		DrawSprite(spritef, pal, x - 14, yf);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   603
		DrawSprite(spriter, pal, x + 15, yr);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   604
	} else {
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   605
		SpriteID sprite = GetRailIcon(engine, false, y);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9020
diff changeset
   606
		DrawSprite(sprite, pal, x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   610
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
   611
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   612
	const RailVehicleInfo *rvi = RailVehInfo(engine);
9923
3056bf623139 (svn r14078) -Codechange: Rename *VehicleInfo::base_cost to cost_factor, indicating what it actually is.
peter1138
parents: 9921
diff changeset
   613
	CommandCost value(EXPENSES_NEW_VEHICLES, (GetEngineProperty(engine, 0x17, rvi->cost_factor) * _price.build_railwagon) >> 8);
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   614
7595
9d645518f001 (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: 7593
diff changeset
   615
	uint num_vehicles = 1 + CountArticulatedParts(engine, false);
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   616
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
	if (!(flags & DC_QUERY_COST)) {
8781
c7bf8db7e611 (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: 8763
diff changeset
   618
		/* Check that the wagon can drive on the track in question */
c7bf8db7e611 (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: 8763
diff changeset
   619
		if (!IsCompatibleRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
c7bf8db7e611 (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: 8763
diff changeset
   620
7595
9d645518f001 (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: 7593
diff changeset
   621
		/* Allow for the wagon and the articulated parts, plus one to "terminate" the list. */
9488
963e9e278125 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 9470
diff changeset
   622
		Vehicle **vl = AllocaM(Vehicle*, num_vehicles + 1);
7595
9d645518f001 (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: 7593
diff changeset
   623
		memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
4831
ffc748f7e3e6 (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
   624
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
   625
		if (!Vehicle::AllocateList(vl, num_vehicles))
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   626
			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
   627
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
		if (flags & DC_EXEC) {
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   629
			Vehicle *v = vl[0];
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   630
			v->spritenum = rvi->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   632
			Vehicle *u = NULL;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   633
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   634
			Vehicle *w;
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   635
			FOR_ALL_VEHICLES(w) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   636
				if (w->type == VEH_TRAIN && w->tile == tile &&
7696
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
   637
				    IsFreeWagon(w) && w->engine_type == engine &&
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
   638
				    !HASBITS(w->vehstatus, VS_CRASHED)) {          /// do not connect new wagon with crashed/flooded consists
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   639
					u = GetLastVehicleInChain(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
7782
e2999b5fcfad (svn r11332) -Fix: vehicles getting a value of 0 on construction.
rubidium
parents: 7696
diff changeset
   644
			v = new (v) Train();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
			v->engine_type = engine;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   647
			DiagDirection dir = GetRailDepotDirection(tile);
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   648
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   649
			v->direction = DiagDirToDir(dir);
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   650
			v->tile = tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   651
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   652
			int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   653
			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
   654
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
			v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
			v->y_pos = y;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   657
			v->z_pos = GetSlopeZ(x, y);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   658
			v->owner = _current_company;
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   659
			v->u.rail.track = TRACK_BIT_DEPOT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
			v->vehstatus = VS_HIDDEN | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   662
//			v->subtype = 0;
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   663
			SetTrainWagon(v);
6771
b58be16fb268 (svn r10007) -Codechange: Add some asserts to IsFrontEngine and friends to ensure that only trains use them.
maedhros
parents: 6752
diff changeset
   664
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   665
			if (u != NULL) {
7493
07944c9e005f (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: 7492
diff changeset
   666
				u->SetNext(v);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   667
			} else {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   668
				SetFreeWagon(v);
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
   669
				InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   670
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   671
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   672
			v->cargo_type = rvi->cargo_type;
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   673
//			v->cargo_subtype = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   674
			v->cargo_cap = rvi->capacity;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   675
			v->value = value.GetCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   676
//			v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   677
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
   678
			v->u.rail.railtype = rvi->railtype;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   679
4329
0e6e689f66e7 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   680
			v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
			v->cur_image = 0xAC2;
2804
ea4080819175 (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
   682
			v->random_bits = VehicleRandomBits();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   683
6643
f81bee57bc09 (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: 6608
diff changeset
   684
			v->group_id = DEFAULT_GROUP;
f81bee57bc09 (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: 6608
diff changeset
   685
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6789
diff changeset
   686
			AddArticulatedParts(vl, VEH_TRAIN);
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   687
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   688
			_new_vehicle_id = v->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
			VehiclePositionChanged(v);
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   691
			TrainConsistChanged(v->First(), false);
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   692
			UpdateTrainGroupID(v->First());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   693
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   695
			if (IsLocalCompany()) {
7425
d9bc116f2f54 (svn r10823) -Fix r8610 [FS#1097]: the autoreplace window vehicle count didn't always update correctly) (Matthias)
bjarni
parents: 7398
diff changeset
   696
				InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
2970
3b9380f10074 (svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents: 2962
diff changeset
   697
			}
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   698
			GetCompany(_current_company)->num_engines[engine]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   699
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8226
diff changeset
   702
	return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   703
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   705
/** Move all free vehicles in the depot to the train */
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   706
static void NormalizeTrainVehInDepot(const Vehicle *u)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
{
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   708
	const Vehicle *v;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   709
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
	FOR_ALL_VEHICLES(v) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   711
		if (v->type == VEH_TRAIN && IsFreeWagon(v) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
				v->tile == u->tile &&
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   713
				v->u.rail.track == TRACK_BIT_DEPOT) {
3491
35d747bb5e82 (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
   714
			if (CmdFailed(DoCommand(0, v->index | (u->index << 16), 1, DC_EXEC,
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   715
					CMD_MOVE_RAIL_VEHICLE)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   721
static CommandCost EstimateTrainCost(EngineID engine, const RailVehicleInfo *rvi)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
{
9923
3056bf623139 (svn r14078) -Codechange: Rename *VehicleInfo::base_cost to cost_factor, indicating what it actually is.
peter1138
parents: 9921
diff changeset
   723
	return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine, 0x17, rvi->cost_factor) * (_price.build_railvehicle >> 3) >> 5);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   726
static void AddRearEngineToMultiheadedTrain(Vehicle *v, Vehicle *u, bool building)
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   727
{
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   728
	u = new (u) Train();
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   729
	u->direction = v->direction;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   730
	u->owner = v->owner;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   731
	u->tile = v->tile;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   732
	u->x_pos = v->x_pos;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   733
	u->y_pos = v->y_pos;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   734
	u->z_pos = v->z_pos;
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   735
	u->u.rail.track = TRACK_BIT_DEPOT;
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   736
	u->vehstatus = v->vehstatus & ~VS_STOPPED;
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   737
//	u->subtype = 0;
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   738
	SetMultiheaded(u);
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   739
	u->spritenum = v->spritenum + 1;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   740
	u->cargo_type = v->cargo_type;
3870
d9ebc4ea750a (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3836
diff changeset
   741
	u->cargo_subtype = v->cargo_subtype;
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   742
	u->cargo_cap = v->cargo_cap;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   743
	u->u.rail.railtype = v->u.rail.railtype;
7493
07944c9e005f (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: 7492
diff changeset
   744
	if (building) v->SetNext(u);
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   745
	u->engine_type = v->engine_type;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   746
	u->build_year = v->build_year;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   747
	if (building) v->value >>= 1;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   748
	u->value = v->value;
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   749
	u->cur_image = 0xAC2;
2804
ea4080819175 (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
   750
	u->random_bits = VehicleRandomBits();
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   751
	VehiclePositionChanged(u);
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   752
}
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   753
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   754
/** Build a railroad vehicle.
3491
35d747bb5e82 (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
   755
 * @param tile tile of the depot where rail-vehicle is built
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   756
 * @param flags type of operation
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   757
 * @param p1 engine type id
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9515
diff changeset
   758
 * @param p2 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
   759
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   760
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
   761
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   762
	/* Check if the engine-type is valid (for the company) */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   763
	if (!IsEngineBuildable(p1, VEH_TRAIN, _current_company)) return_cmd_error(STR_RAIL_VEHICLE_NOT_AVAILABLE);
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1192
diff changeset
   764
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   765
	/* Check if the train is actually being built in a depot belonging
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   766
	 * to the company. Doesn't matter if only the cost is queried */
1443
2b8e1b123d91 (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
   767
	if (!(flags & DC_QUERY_COST)) {
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   768
		if (!IsRailDepotTile(tile)) return CMD_ERROR;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   769
		if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
1443
2b8e1b123d91 (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
   770
	}
1221
ce638de43f3a (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
   771
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   772
	const RailVehicleInfo *rvi = RailVehInfo(p1);
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   773
5868
94430141c189 (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: 5858
diff changeset
   774
	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
   775
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   776
	CommandCost value = EstimateTrainCost(p1, rvi);
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   777
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   778
	uint num_vehicles =
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   779
		(rvi->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) +
7595
9d645518f001 (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: 7593
diff changeset
   780
		CountArticulatedParts(p1, false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   781
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   782
	if (!(flags & DC_QUERY_COST)) {
8781
c7bf8db7e611 (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: 8763
diff changeset
   783
		/* Check if depot and new engine uses the same kind of tracks *
c7bf8db7e611 (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: 8763
diff changeset
   784
		 * We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */
c7bf8db7e611 (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: 8763
diff changeset
   785
		if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
c7bf8db7e611 (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: 8763
diff changeset
   786
7595
9d645518f001 (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: 7593
diff changeset
   787
		/* Allow for the dual-heads and the articulated parts, plus one to "terminate" the list. */
9488
963e9e278125 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 9470
diff changeset
   788
		Vehicle **vl = AllocaM(Vehicle*, num_vehicles + 1);
7595
9d645518f001 (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: 7593
diff changeset
   789
		memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
4831
ffc748f7e3e6 (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
   790
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   791
		if (!Vehicle::AllocateList(vl, num_vehicles)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   793
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   795
		Vehicle *v = vl[0];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   796
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9515
diff changeset
   797
		UnitID unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_TRAIN);
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   798
		if (unit_num > _settings_game.vehicle.max_trains)
3816
28ee8b8c2522 (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
   799
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
28ee8b8c2522 (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
   800
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
		if (flags & DC_EXEC) {
3491
35d747bb5e82 (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
   802
			DiagDirection dir = GetRailDepotDirection(tile);
35d747bb5e82 (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
   803
			int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir];
35d747bb5e82 (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
   804
			int y = TileY(tile) * TILE_SIZE + _vehicle_initial_y_fract[dir];
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   805
7783
b6c199abdd23 (svn r11333) -Fix: Train engines still had a value of 0.
maedhros
parents: 7782
diff changeset
   806
			v = new (v) Train();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
			v->unitnumber = unit_num;
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   808
			v->direction = DiagDirToDir(dir);
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   809
			v->tile = tile;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   810
			v->owner = _current_company;
3491
35d747bb5e82 (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
   811
			v->x_pos = x;
35d747bb5e82 (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
   812
			v->y_pos = y;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   813
			v->z_pos = GetSlopeZ(x, y);
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   814
//			v->running_ticks = 0;
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   815
			v->u.rail.track = TRACK_BIT_DEPOT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
			v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
			v->spritenum = rvi->image_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
			v->cargo_type = rvi->cargo_type;
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   819
//			v->cargo_subtype = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
			v->cargo_cap = rvi->capacity;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
			v->max_speed = rvi->max_speed;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   822
			v->value = value.GetCost();
1266
eccd576e322f (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   823
			v->last_station_visited = INVALID_STATION;
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   824
//			v->dest_tile = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   825
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   826
			v->engine_type = p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
   828
			const Engine *e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
			v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
			v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
			v->max_age = e->lifelength * 366;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   832
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8257
diff changeset
   833
			v->name = NULL;
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
   834
			v->u.rail.railtype = rvi->railtype;
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   835
			_new_vehicle_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   836
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   837
			v->service_interval = _settings_game.vehicle.servint_trains;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
			v->date_of_last_service = _date;
4329
0e6e689f66e7 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   839
			v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
			v->cur_image = 0xAC2;
2804
ea4080819175 (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
   841
			v->random_bits = VehicleRandomBits();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   843
//			v->vehicle_flags = 0;
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
   844
			if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
6176
153f0f24dc1b (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6175
diff changeset
   845
6643
f81bee57bc09 (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: 6608
diff changeset
   846
			v->group_id = DEFAULT_GROUP;
f81bee57bc09 (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: 6608
diff changeset
   847
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   848
//			v->subtype = 0;
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   849
			SetFrontEngine(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   850
			SetTrainEngine(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   851
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
			VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   853
5868
94430141c189 (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: 5858
diff changeset
   854
			if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   855
				SetMultiheaded(v);
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   856
				AddRearEngineToMultiheadedTrain(vl[0], vl[1], true);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   857
				/* Now we need to link the front and rear engines together
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   858
				 * other_multiheaded_part is the pointer that links to the other half of the engine
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   859
				 * vl[0] is the front and vl[1] is the rear
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   860
				 */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   861
				vl[0]->u.rail.other_multiheaded_part = vl[1];
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   862
				vl[1]->u.rail.other_multiheaded_part = vl[0];
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   863
			} else {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6789
diff changeset
   864
				AddArticulatedParts(vl, VEH_TRAIN);
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   865
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
   867
			TrainConsistChanged(v, false);
6643
f81bee57bc09 (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: 6608
diff changeset
   868
			UpdateTrainGroupID(v);
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   869
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9515
diff changeset
   870
			if (!HasBit(p2, 1) && !(flags & DC_AUTOREPLACE)) { // check if the cars should be added to the new vehicle
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   871
				NormalizeTrainVehInDepot(v);
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   872
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
   874
			InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9274
diff changeset
   875
			InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
			InvalidateWindow(WC_COMPANY, v->owner);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   877
			if (IsLocalCompany()) {
7425
d9bc116f2f54 (svn r10823) -Fix r8610 [FS#1097]: the autoreplace window vehicle count didn't always update correctly) (Matthias)
bjarni
parents: 7398
diff changeset
   878
				InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
   879
			}
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5922
diff changeset
   880
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
   881
			GetCompany(_current_company)->num_engines[p1]++;
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
	}
1128
ca7f860db7ac (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
   884
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   885
	return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   888
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   889
/* Check if all the wagons of the given train are in a depot, returns the
3183
90c676e6a50d (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
   890
 * number of cars (including loco) then. If not it returns -1 */
4648
7bde2a5c36e8 (svn r6524) -Code cleanup r6515: cleaned up the command to start/stop all vehicles in a depot.
bjarni
parents: 4632
diff changeset
   891
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
   892
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
	TileIndex tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   894
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
	/* check if stopped in a depot */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   896
	if (!IsRailDepotTile(tile) || v->cur_speed != 0) return -1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   898
	int count = 0;
7492
35acee076719 (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: 7488
diff changeset
   899
	for (; v != NULL; v = v->Next()) {
2844
3c0e05cc5f1a (svn r3392) Train depot fixes:
peter1138
parents: 2840
diff changeset
   900
		/* This count is used by the depot code to determine the number of engines
3c0e05cc5f1a (svn r3392) Train depot fixes:
peter1138
parents: 2840
diff changeset
   901
		 * in the consist. Exclude articulated parts so that autoreplacing to
3986
1390892ea4f3 (svn r5176) fixing the typo, that should have been fixed in last commit
bjarni
parents: 3985
diff changeset
   902
		 * engines with more articulated parts than before works correctly.
3985
5dc801277eea (svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents: 3977
diff changeset
   903
		 *
5dc801277eea (svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents: 3977
diff changeset
   904
		 * Also skip counting rear ends of multiheaded engines */
7526
27c4fbf8aef0 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni
parents: 7502
diff changeset
   905
		if (!IsArticulatedPart(v) && !IsRearDualheaded(v)) count++;
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   906
		if (v->u.rail.track != TRACK_BIT_DEPOT || v->tile != tile ||
4529
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   907
				(IsFrontEngine(v) && needs_to_be_stopped && !(v->vehstatus & VS_STOPPED))) {
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   908
			return -1;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   909
		}
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   910
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
	return count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
4529
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   915
/* Used to check if the train is inside the depot and verifying that the VS_STOPPED flag is set */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   916
int CheckTrainStoppedInDepot(const Vehicle *v)
4529
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   917
{
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   918
	return CheckTrainInDepot(v, true);
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   919
}
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   920
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   921
/* Used to check if the train is inside the depot, but not checking the VS_STOPPED flag */
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   922
inline bool CheckTrainIsInsideDepot(const Vehicle *v)
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   923
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   924
	return CheckTrainInDepot(v, false) > 0;
4529
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   925
}
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   926
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   927
/**
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   928
 * Unlink a rail wagon from the consist.
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   929
 * @param v Vehicle to remove.
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   930
 * @param first The first vehicle of the consist.
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   931
 * @return The first vehicle of the consist.
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   932
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   935
	/* unlinking the first vehicle of the chain? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
	if (v == first) {
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   937
		v = GetNextVehicle(v);
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   938
		if (v == NULL) return NULL;
1766
8cdb302ae946 (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
   939
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   940
		if (IsTrainWagon(v)) SetFreeWagon(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   941
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
		return v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
	}
1766
8cdb302ae946 (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
   944
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   945
	Vehicle *u;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   946
	for (u = first; GetNextVehicle(u) != v; u = GetNextVehicle(u)) {}
7493
07944c9e005f (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: 7492
diff changeset
   947
	GetLastEnginePart(u)->SetNext(GetNextVehicle(v));
1766
8cdb302ae946 (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
   948
	return first;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   951
static Vehicle *FindGoodVehiclePos(const Vehicle *src)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
	Vehicle *dst;
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   954
	EngineID eng = src->engine_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
	TileIndex tile = src->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
	FOR_ALL_VEHICLES(dst) {
7696
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
   958
		if (dst->type == VEH_TRAIN && IsFreeWagon(dst) && dst->tile == tile && !HASBITS(dst->vehstatus, VS_CRASHED)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   959
			/* 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
   960
			Vehicle *v = dst;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   961
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
			while (v->engine_type == eng) {
7492
35acee076719 (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: 7488
diff changeset
   963
				v = v->Next();
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   964
				if (v == NULL) return dst;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   972
/*
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   973
 * add a vehicle v behind vehicle dest
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   974
 * use this function since it sets flags as needed
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   975
 */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   976
static void AddWagonToConsist(Vehicle *v, Vehicle *dest)
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   977
{
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   978
	UnlinkWagon(v, v->First());
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   979
	if (dest == NULL) return;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   980
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   981
	Vehicle *next = dest->Next();
8007
f89f5691ce89 (svn r11567) -Fix [FS#1512] (r11011): break the chain before moving a vehicle after another in the same chain
glx
parents: 7967
diff changeset
   982
	v->SetNext(NULL);
7492
35acee076719 (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: 7488
diff changeset
   983
	dest->SetNext(v);
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   984
	v->SetNext(next);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   985
	ClearFreeWagon(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   986
	ClearFrontEngine(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   987
}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   988
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   989
/*
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   990
 * move around on the train so rear engines are placed correctly according to the other engines
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   991
 * always call with the front engine
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   992
 */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   993
static void NormaliseTrainConsist(Vehicle *v)
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   994
{
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   995
	if (IsFreeWagon(v)) return;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   996
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   997
	assert(IsFrontEngine(v));
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   998
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   999
	for (; v != NULL; v = GetNextVehicle(v)) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1000
		if (!IsMultiheaded(v) || !IsTrainEngine(v)) continue;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1001
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1002
		/* make sure that there are no free cars before next engine */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1003
		Vehicle *u;
7492
35acee076719 (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: 7488
diff changeset
  1004
		for (u = v; u->Next() != NULL && !IsTrainEngine(u->Next()); u = u->Next()) {}
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1005
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1006
		if (u == v->u.rail.other_multiheaded_part) continue;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1007
		AddWagonToConsist(v->u.rail.other_multiheaded_part, u);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1008
	}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1009
}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1010
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1011
/** Move a rail vehicle around inside the depot.
3491
35d747bb5e82 (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
  1012
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1013
 * @param flags type of operation
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9923
diff changeset
  1014
 *              Note: DC_AUTOREPLACE is set when autoreplace tries to undo its modifications or moves vehicles to temporary locations inside the depot.
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1015
 * @param p1 various bitstuffed elements
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1016
 * - p1 (bit  0 - 15) source vehicle index
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1017
 * - p1 (bit 16 - 31) what wagon to put the source wagon AFTER, XXX - INVALID_VEHICLE to make a new line
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1018
 * @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
  1019
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1020
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
  1021
{
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1022
	VehicleID s = GB(p1, 0, 16);
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1023
	VehicleID d = GB(p1, 16, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1025
	if (!IsValidVehicleID(s)) return CMD_ERROR;
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1026
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1027
	Vehicle *src = GetVehicle(s);
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1028
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1029
	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
  1030
7696
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
  1031
	/* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
  1032
	if (HASBITS(src->vehstatus, VS_CRASHED)) return CMD_ERROR;
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
  1033
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1034
	/* if nothing is selected as destination, try and find a matching vehicle to drag to. */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1035
	Vehicle *dst;
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1036
	if (d == INVALID_VEHICLE) {
4198
f8d95f4d9af1 (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1037
		dst = IsTrainEngine(src) ? NULL : FindGoodVehiclePos(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
	} else {
6136
6a4ee6549aaf (svn r8878) -Fix
tron
parents: 6114
diff changeset
  1039
		if (!IsValidVehicleID(d)) return CMD_ERROR;
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  1040
		dst = GetVehicle(d);
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1041
		if (dst->type != VEH_TRAIN || !CheckOwnership(dst->owner)) return CMD_ERROR;
7696
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
  1042
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
  1043
		/* Do not allow appending to crashed vehicles, too */
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
  1044
		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
  1045
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1046
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1047
	/* if an articulated part is being handled, deal with its parent vehicle */
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1048
	while (IsArticulatedPart(src)) src = src->Previous();
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1049
	if (dst != NULL) {
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1050
		while (IsArticulatedPart(dst)) dst = dst->Previous();
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1051
	}
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1052
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1053
	/* don't move the same vehicle.. */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1054
	if (src == dst) return CommandCost();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1055
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
	/* locate the head of the two chains */
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1057
	Vehicle *src_head = src->First();
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1058
	Vehicle *dst_head;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1059
	if (dst != NULL) {
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1060
		dst_head = dst->First();
6136
6a4ee6549aaf (svn r8878) -Fix
tron
parents: 6114
diff changeset
  1061
		if (dst_head->tile != src_head->tile) return CMD_ERROR;
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1062
		/* Now deal with articulated part of destination wagon */
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1063
		dst = GetLastEnginePart(dst);
6136
6a4ee6549aaf (svn r8878) -Fix
tron
parents: 6114
diff changeset
  1064
	} else {
6a4ee6549aaf (svn r8878) -Fix
tron
parents: 6114
diff changeset
  1065
		dst_head = NULL;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1066
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1067
7526
27c4fbf8aef0 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni
parents: 7502
diff changeset
  1068
	if (IsRearDualheaded(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1069
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1070
	/* when moving all wagons, we can't have the same src_head and dst_head */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1071
	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
  1072
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9923
diff changeset
  1073
	/* check if all vehicles in the source train are stopped inside a depot. */
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9923
diff changeset
  1074
	int src_len = CheckTrainStoppedInDepot(src_head);
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9923
diff changeset
  1075
	if (src_len < 0) return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED);
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9923
diff changeset
  1076
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9923
diff changeset
  1077
	if ((flags & DC_AUTOREPLACE) == 0) {
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9923
diff changeset
  1078
		/* Check whether there are more than 'max_len' train units (articulated parts and rear heads do not count) in the new chain */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  1079
		int max_len = _settings_game.vehicle.mammoth_trains ? 100 : 10;
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1080
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1081
		/* check the destination row if the source and destination aren't the same. */
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1082
		if (src_head != dst_head) {
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1083
			int dst_len = 0;
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1084
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1085
			if (dst_head != NULL) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1086
				/* check if all vehicles in the dest train are stopped. */
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1087
				dst_len = CheckTrainStoppedInDepot(dst_head);
3183
90c676e6a50d (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
  1088
				if (dst_len < 0) return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED);
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1089
			}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1090
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1091
			/* We are moving between rows, so only count the wagons from the source
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1092
			 * row that are being moved. */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1093
			if (HasBit(p2, 0)) {
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1094
				const Vehicle *u;
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1095
				for (u = src_head; u != src && u != NULL; u = GetNextVehicle(u))
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1096
					src_len--;
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1097
			} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1098
				/* If moving only one vehicle, just count that. */
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1099
				src_len = 1;
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1100
			}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1101
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1102
			if (src_len + dst_len > max_len) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1103
				/* Abort if we're adding too many wagons to a train. */
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1104
				if (dst_head != NULL && IsFrontEngine(dst_head)) return_cmd_error(STR_8819_TRAIN_TOO_LONG);
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1105
				/* Abort if we're making a train on a new row. */
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1106
				if (dst_head == NULL && IsTrainEngine(src)) return_cmd_error(STR_8819_TRAIN_TOO_LONG);
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1107
			}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1108
		} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1109
			/* Abort if we're creating a new train on an existing row. */
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1110
			if (src_len > max_len && src == src_head && IsTrainEngine(GetNextVehicle(src_head)))
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1111
				return_cmd_error(STR_8819_TRAIN_TOO_LONG);
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1112
		}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1113
	}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1114
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1115
	/* moving a loco to a new line?, then we need to assign a unitnumber. */
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1116
	if (dst == NULL && !IsFrontEngine(src) && IsTrainEngine(src)) {
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9923
diff changeset
  1117
		UnitID unit_num = ((flags & DC_AUTOREPLACE) != 0 ? 0 : GetFreeUnitNumber(VEH_TRAIN));
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  1118
		if (unit_num > _settings_game.vehicle.max_trains)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
			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
  1120
3017
a75caf4efa2d (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
  1121
		if (flags & DC_EXEC) src->unitnumber = unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
10178
15d8196fed65 (svn r14384) -Fix: Moving the first vehicle of a train elsewhere might require a new unitnumber for the remaining chain.
frosch
parents: 10176
diff changeset
  1124
	/* When we move the front vehicle, the second vehicle might need a unitnumber */
10190
9915a44e1459 (svn r14403) -Fix (r14384): The condition was too restrictive.
frosch
parents: 10188
diff changeset
  1125
	if (!HasBit(p2, 0) && (IsFreeWagon(src) || (IsFrontEngine(src) && dst == NULL)) && (flags & DC_AUTOREPLACE) == 0) {
10178
15d8196fed65 (svn r14384) -Fix: Moving the first vehicle of a train elsewhere might require a new unitnumber for the remaining chain.
frosch
parents: 10176
diff changeset
  1126
		Vehicle *second = GetNextUnit(src);
15d8196fed65 (svn r14384) -Fix: Moving the first vehicle of a train elsewhere might require a new unitnumber for the remaining chain.
frosch
parents: 10176
diff changeset
  1127
		if (second != NULL && IsTrainEngine(second) && GetFreeUnitNumber(VEH_TRAIN) > _settings_game.vehicle.max_trains) {
15d8196fed65 (svn r14384) -Fix: Moving the first vehicle of a train elsewhere might require a new unitnumber for the remaining chain.
frosch
parents: 10176
diff changeset
  1128
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
15d8196fed65 (svn r14384) -Fix: Moving the first vehicle of a train elsewhere might require a new unitnumber for the remaining chain.
frosch
parents: 10176
diff changeset
  1129
		}
15d8196fed65 (svn r14384) -Fix: Moving the first vehicle of a train elsewhere might require a new unitnumber for the remaining chain.
frosch
parents: 10176
diff changeset
  1130
	}
15d8196fed65 (svn r14384) -Fix: Moving the first vehicle of a train elsewhere might require a new unitnumber for the remaining chain.
frosch
parents: 10176
diff changeset
  1131
8162
70bce0f4e095 (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: 8157
diff changeset
  1132
	/*
70bce0f4e095 (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: 8157
diff changeset
  1133
	 * Check whether the vehicles in the source chain are in the destination
70bce0f4e095 (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: 8157
diff changeset
  1134
	 * chain. This can easily be done by checking whether the first vehicle
70bce0f4e095 (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: 8157
diff changeset
  1135
	 * of the source chain is in the destination chain as the Next/Previous
70bce0f4e095 (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: 8157
diff changeset
  1136
	 * pointers always make a doubly linked list of it where the assumption
70bce0f4e095 (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: 8157
diff changeset
  1137
	 * v->Next()->Previous() == v holds (assuming v->Next() != NULL).
70bce0f4e095 (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: 8157
diff changeset
  1138
	 */
70bce0f4e095 (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: 8157
diff changeset
  1139
	bool src_in_dst = false;
70bce0f4e095 (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: 8157
diff changeset
  1140
	for (Vehicle *v = dst_head; !src_in_dst && v != NULL; v = v->Next()) src_in_dst = v == src;
70bce0f4e095 (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: 8157
diff changeset
  1141
70bce0f4e095 (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: 8157
diff changeset
  1142
	/*
70bce0f4e095 (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: 8157
diff changeset
  1143
	 * If the source chain is in the destination chain then the user is
70bce0f4e095 (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: 8157
diff changeset
  1144
	 * only reordering the vehicles, thus not attaching a new vehicle.
70bce0f4e095 (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: 8157
diff changeset
  1145
	 * Therefor the 'allow wagon attach' callback does not need to be
70bce0f4e095 (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: 8157
diff changeset
  1146
	 * called. If it would be called strange things would happen because
70bce0f4e095 (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: 8157
diff changeset
  1147
	 * one 'attaches' an already 'attached' vehicle causing more trouble
70bce0f4e095 (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: 8157
diff changeset
  1148
	 * than it actually solves (infinite loops and such).
70bce0f4e095 (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: 8157
diff changeset
  1149
	 */
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9923
diff changeset
  1150
	if (dst_head != NULL && !src_in_dst && (flags & DC_AUTOREPLACE) == 0) {
8162
70bce0f4e095 (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: 8157
diff changeset
  1151
		/*
70bce0f4e095 (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: 8157
diff changeset
  1152
		 * When performing the 'allow wagon attach' callback, we have to check
70bce0f4e095 (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: 8157
diff changeset
  1153
		 * that for each and every wagon, not only the first one. This means
70bce0f4e095 (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: 8157
diff changeset
  1154
		 * that we have to test one wagon, attach it to the train and then test
70bce0f4e095 (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: 8157
diff changeset
  1155
		 * the next wagon till we have reached the end. We have to restore it
70bce0f4e095 (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: 8157
diff changeset
  1156
		 * to the state it was before we 'tried' attaching the train when the
70bce0f4e095 (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: 8157
diff changeset
  1157
		 * attaching fails or succeeds because we are not 'only' doing this
70bce0f4e095 (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: 8157
diff changeset
  1158
		 * in the DC_EXEC state.
70bce0f4e095 (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: 8157
diff changeset
  1159
		 */
70bce0f4e095 (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: 8157
diff changeset
  1160
		Vehicle *dst_tail = dst_head;
70bce0f4e095 (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: 8157
diff changeset
  1161
		while (dst_tail->Next() != NULL) dst_tail = dst_tail->Next();
70bce0f4e095 (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: 8157
diff changeset
  1162
70bce0f4e095 (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: 8157
diff changeset
  1163
		Vehicle *orig_tail = dst_tail;
70bce0f4e095 (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: 8157
diff changeset
  1164
		Vehicle *next_to_attach = src;
70bce0f4e095 (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: 8157
diff changeset
  1165
		Vehicle *src_previous = src->Previous();
70bce0f4e095 (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: 8157
diff changeset
  1166
70bce0f4e095 (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: 8157
diff changeset
  1167
		while (next_to_attach != NULL) {
10188
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1168
			/* Don't check callback for articulated or rear dual headed parts */
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1169
			if (!IsArticulatedPart(next_to_attach) && !IsRearDualheaded(next_to_attach)) {
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1170
				/* Back up and clear the first_engine data to avoid using wagon override group */
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1171
				EngineID first_engine = next_to_attach->u.rail.first_engine;
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1172
				next_to_attach->u.rail.first_engine = INVALID_ENGINE;
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1173
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1174
				uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, dst_head->engine_type, next_to_attach, dst_head);
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1175
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1176
				/* Restore original first_engine data */
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1177
				next_to_attach->u.rail.first_engine = first_engine;
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1178
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1179
				if (callback != CALLBACK_FAILED) {
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1180
					StringID error = STR_NULL;
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1181
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1182
					if (callback == 0xFD) error = STR_INCOMPATIBLE_RAIL_TYPES;
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1183
					if (callback < 0xFD) error = GetGRFStringID(GetEngineGRFID(dst_head->engine_type), 0xD000 + callback);
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1184
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1185
					if (error != STR_NULL) {
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1186
						/*
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1187
						 * The attaching is not allowed. In this case 'next_to_attach'
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1188
						 * can contain some vehicles of the 'source' and the destination
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1189
						 * train can have some too. We 'just' add the to-be added wagons
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1190
						 * to the chain and then split it where it was previously
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1191
						 * separated, i.e. the tail of the original destination train.
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1192
						 * Furthermore the 'previous' link of the original source vehicle needs
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1193
						 * to be restored, otherwise the train goes missing in the depot.
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1194
						 */
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1195
						dst_tail->SetNext(next_to_attach);
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1196
						orig_tail->SetNext(NULL);
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1197
						if (src_previous != NULL) src_previous->SetNext(src);
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1198
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1199
						return_cmd_error(error);
a418756accd1 (svn r14401) -Fix (r11724): Don't check articulated parts or rear multihead parts for callback 1D when moving a chain of wagons.
peter1138
parents: 10186
diff changeset
  1200
					}
8162
70bce0f4e095 (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: 8157
diff changeset
  1201
				}
3727
3a3ebb0bde7e (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
  1202
			}
8162
70bce0f4e095 (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: 8157
diff changeset
  1203
8168
a1351d83d0d4 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8162
diff changeset
  1204
			/* Only check further wagons if told to move the chain */
a1351d83d0d4 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8162
diff changeset
  1205
			if (!HasBit(p2, 0)) break;
a1351d83d0d4 (svn r11731) -Fix [FS#1582]: Clear sprite override data before performing NewGRF wagon attach callback. This stopped
peter1138
parents: 8162
diff changeset
  1206
8162
70bce0f4e095 (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: 8157
diff changeset
  1207
			/*
70bce0f4e095 (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: 8157
diff changeset
  1208
			 * Adding a next wagon to the chain so we can test the other wagons.
70bce0f4e095 (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: 8157
diff changeset
  1209
			 * First 'take' the first wagon from 'next_to_attach' and move it
70bce0f4e095 (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: 8157
diff changeset
  1210
			 * to the next wagon. Then add that to the tail of the destination
70bce0f4e095 (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: 8157
diff changeset
  1211
			 * train and update the tail with the new vehicle.
70bce0f4e095 (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: 8157
diff changeset
  1212
			 */
70bce0f4e095 (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: 8157
diff changeset
  1213
			Vehicle *to_add = next_to_attach;
70bce0f4e095 (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: 8157
diff changeset
  1214
			next_to_attach = next_to_attach->Next();
70bce0f4e095 (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: 8157
diff changeset
  1215
70bce0f4e095 (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: 8157
diff changeset
  1216
			to_add->SetNext(NULL);
70bce0f4e095 (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: 8157
diff changeset
  1217
			dst_tail->SetNext(to_add);
70bce0f4e095 (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: 8157
diff changeset
  1218
			dst_tail = dst_tail->Next();
3727
3a3ebb0bde7e (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
  1219
		}
8162
70bce0f4e095 (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: 8157
diff changeset
  1220
70bce0f4e095 (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: 8157
diff changeset
  1221
		/*
70bce0f4e095 (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: 8157
diff changeset
  1222
		 * When we reach this the attaching is allowed. It also means that the
70bce0f4e095 (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: 8157
diff changeset
  1223
		 * chain of vehicles to attach is empty, so we do not need to merge that.
70bce0f4e095 (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: 8157
diff changeset
  1224
		 * This means only the splitting needs to be done.
70bce0f4e095 (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: 8157
diff changeset
  1225
		 * Furthermore the 'previous' link of the original source vehicle needs
70bce0f4e095 (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: 8157
diff changeset
  1226
		 * to be restored, otherwise the train goes missing in the depot.
70bce0f4e095 (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: 8157
diff changeset
  1227
		 */
70bce0f4e095 (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: 8157
diff changeset
  1228
		orig_tail->SetNext(NULL);
70bce0f4e095 (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: 8157
diff changeset
  1229
		if (src_previous != NULL) src_previous->SetNext(src);
3727
3a3ebb0bde7e (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
  1230
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1231
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1232
	/* do it? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1233
	if (flags & DC_EXEC) {
6643
f81bee57bc09 (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: 6608
diff changeset
  1234
		/* If we move the front Engine and if the second vehicle is not an engine
f81bee57bc09 (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: 6608
diff changeset
  1235
		   add the whole vehicle to the DEFAULT_GROUP */
f81bee57bc09 (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: 6608
diff changeset
  1236
		if (IsFrontEngine(src) && !IsDefaultGroupID(src->group_id)) {
7529
ca42b9956322 (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 7526
diff changeset
  1237
			Vehicle *v = GetNextVehicle(src);
ca42b9956322 (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 7526
diff changeset
  1238
ca42b9956322 (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 7526
diff changeset
  1239
			if (v != NULL && IsTrainEngine(v)) {
ca42b9956322 (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 7526
diff changeset
  1240
				v->group_id   = src->group_id;
ca42b9956322 (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 7526
diff changeset
  1241
				src->group_id = DEFAULT_GROUP;
6643
f81bee57bc09 (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: 6608
diff changeset
  1242
			}
f81bee57bc09 (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: 6608
diff changeset
  1243
		}
f81bee57bc09 (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: 6608
diff changeset
  1244
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1245
		if (HasBit(p2, 0)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1246
			/* unlink ALL wagons */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1247
			if (src != src_head) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1248
				Vehicle *v = src_head;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1249
				while (GetNextVehicle(v) != src) v = GetNextVehicle(v);
7493
07944c9e005f (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: 7492
diff changeset
  1250
				GetLastEnginePart(v)->SetNext(NULL);
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1251
			} else {
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1252
				InvalidateWindowData(WC_VEHICLE_DEPOT, src_head->tile); // We removed a line
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1253
				src_head = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1255
		} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1256
			/* if moving within the same chain, dont use dst_head as it may get invalidated */
4198
f8d95f4d9af1 (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1257
			if (src_head == dst_head) dst_head = NULL;
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1258
			/* unlink single wagon from linked list */
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1259
			src_head = UnlinkWagon(src, src_head);
7493
07944c9e005f (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: 7492
diff changeset
  1260
			GetLastEnginePart(src)->SetNext(NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1262
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
		if (dst == NULL) {
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1264
			/* We make a new line in the depot, so we know already that we invalidate the window data */
4806
2e9d9fc049b0 (svn r6728) -Fix r6651: solved issue where moving rail vehicles in a depot could read from a NULL pointer
bjarni
parents: 4739
diff changeset
  1265
			InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1266
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1267
			/* move the train to an empty line. for locomotives, we set the type to TS_Front. for wagons, 4. */
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1268
			if (IsTrainEngine(src)) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1269
				if (!IsFrontEngine(src)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1270
					/* setting the type to 0 also involves setting up the orders field. */
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1271
					SetFrontEngine(src);
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1272
					assert(src->orders == NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
					src->num_orders = 0;
6643
f81bee57bc09 (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: 6608
diff changeset
  1274
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1275
					/* Decrease the engines number of the src engine_type */
6643
f81bee57bc09 (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: 6608
diff changeset
  1276
					if (!IsDefaultGroupID(src->group_id) && IsValidGroupID(src->group_id)) {
f81bee57bc09 (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: 6608
diff changeset
  1277
						GetGroup(src->group_id)->num_engines[src->engine_type]--;
f81bee57bc09 (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: 6608
diff changeset
  1278
					}
f81bee57bc09 (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: 6608
diff changeset
  1279
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1280
					/* If we move an engine to a new line affect it to the DEFAULT_GROUP */
6643
f81bee57bc09 (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: 6608
diff changeset
  1281
					src->group_id = DEFAULT_GROUP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1282
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1283
			} else {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1284
				SetFreeWagon(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1285
			}
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1286
			dst_head = src;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
		} else {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1288
			if (IsFrontEngine(src)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1289
				/* 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
  1290
				DeleteWindowById(WC_VEHICLE_VIEW, src->index);
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1291
				DeleteVehicleOrders(src);
7529
ca42b9956322 (svn r11048) -Fix [FS#1206]: several cases where (re)moving eninges on a train would corrupt the vehicle counts for the groups.
rubidium
parents: 7526
diff changeset
  1292
				RemoveVehicleFromGroup(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1293
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1294
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1295
			if (IsFrontEngine(src) || IsFreeWagon(src)) {
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1296
				InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1297
				ClearFrontEngine(src);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1298
				ClearFreeWagon(src);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1299
				src->unitnumber = 0; // doesn't occupy a unitnumber anymore.
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1300
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1301
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1302
			/* link in the wagon(s) in the chain. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1303
			{
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1304
				Vehicle *v;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1305
8695
6607e9b9ffe2 (svn r12368) -Codechange: use explicit body for loops and conditions and remove -Wno-empty-body from the configure script
smatz
parents: 8667
diff changeset
  1306
				for (v = src; GetNextVehicle(v) != NULL; v = GetNextVehicle(v)) {}
7492
35acee076719 (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: 7488
diff changeset
  1307
				GetLastEnginePart(v)->SetNext(dst->Next());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1308
			}
7492
35acee076719 (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: 7488
diff changeset
  1309
			dst->SetNext(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1310
		}
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1311
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1312
		if (src->u.rail.other_multiheaded_part != NULL) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1313
			if (src->u.rail.other_multiheaded_part == src_head) {
7492
35acee076719 (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: 7488
diff changeset
  1314
				src_head = src_head->Next();
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1315
			}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1316
			AddWagonToConsist(src->u.rail.other_multiheaded_part, src);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1317
		}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1318
2607
9915a4829de8 (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
  1319
		/* If there is an engine behind first_engine we moved away, it should become new first_engine
9915a4829de8 (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
  1320
		 * To do this, CmdMoveRailVehicle must be called once more
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1321
		 * we can't loop forever here because next time we reach this line we will have a front engine */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1322
		if (src_head != NULL && !IsFrontEngine(src_head) && IsTrainEngine(src_head)) {
6643
f81bee57bc09 (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: 6608
diff changeset
  1323
			/* As in CmdMoveRailVehicle src_head->group_id will be equal to DEFAULT_GROUP
f81bee57bc09 (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: 6608
diff changeset
  1324
			 * we need to save the group and reaffect it to src_head */
f81bee57bc09 (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: 6608
diff changeset
  1325
			const GroupID tmp_g = src_head->group_id;
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3487
diff changeset
  1326
			CmdMoveRailVehicle(0, flags, src_head->index | (INVALID_VEHICLE << 16), 1);
6643
f81bee57bc09 (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: 6608
diff changeset
  1327
			SetTrainGroupID(src_head, tmp_g);
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  1328
			src_head = NULL; // don't do anything more to this train since the new call will do it
2607
9915a4829de8 (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
  1329
		}
9915a4829de8 (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
  1330
4198
f8d95f4d9af1 (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1331
		if (src_head != NULL) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1332
			NormaliseTrainConsist(src_head);
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
  1333
			TrainConsistChanged(src_head, false);
6643
f81bee57bc09 (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: 6608
diff changeset
  1334
			UpdateTrainGroupID(src_head);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1335
			if (IsFrontEngine(src_head)) {
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1336
				/* Update the refit button and window */
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1337
				InvalidateWindow(WC_VEHICLE_REFIT, src_head->index);
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8344
diff changeset
  1338
				InvalidateWindowWidget(WC_VEHICLE_VIEW, src_head->index, VVW_WIDGET_REFIT_VEH);
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1339
			}
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1340
			/* Update the depot window */
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1341
			InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile);
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1342
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
4198
f8d95f4d9af1 (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1344
		if (dst_head != NULL) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1345
			NormaliseTrainConsist(dst_head);
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
  1346
			TrainConsistChanged(dst_head, false);
6643
f81bee57bc09 (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: 6608
diff changeset
  1347
			UpdateTrainGroupID(dst_head);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1348
			if (IsFrontEngine(dst_head)) {
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1349
				/* Update the refit button and window */
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8344
diff changeset
  1350
				InvalidateWindowWidget(WC_VEHICLE_VIEW, dst_head->index, VVW_WIDGET_REFIT_VEH);
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1351
				InvalidateWindow(WC_VEHICLE_REFIT, dst_head->index);
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1352
			}
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1353
			/* Update the depot window */
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1354
			InvalidateWindow(WC_VEHICLE_DEPOT, dst_head->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1355
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1356
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9274
diff changeset
  1357
		InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1358
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1359
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1360
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1361
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1362
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1363
/** Sell a (single) train wagon/engine.
3491
35d747bb5e82 (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
  1364
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1365
 * @param flags type of operation
1766
8cdb302ae946 (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
 * @param p1 the wagon/engine index
8cdb302ae946 (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
 * @param p2 the selling mode
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1368
 * - p2 = 0: only sell the single dragged wagon/engine (and any belonging rear-engines)
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1369
 * - p2 = 1: sell the vehicle and all vehicles following it in the chain
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8962
diff changeset
  1370
 *           if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1371
 * - p2 = 2: when selling attached locos, rearrange all vehicles after it to separate lines;
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1372
 *           all wagons of the same type will go on the same line. Used by the AI currently
1766
8cdb302ae946 (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
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1374
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
  1375
{
6789
d32dd7c82158 (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: 6788
diff changeset
  1376
	/* Check if we deleted a vehicle window */
d32dd7c82158 (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: 6788
diff changeset
  1377
	Window *w = NULL;
d32dd7c82158 (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: 6788
diff changeset
  1378
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1379
	if (!IsValidVehicleID(p1) || p2 > 2) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1380
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1381
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1382
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1383
	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
  1384
7695
4b27fd86b318 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 7642
diff changeset
  1385
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
4b27fd86b318 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 7642
diff changeset
  1386
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1387
	while (IsArticulatedPart(v)) v = v->Previous();
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1388
	Vehicle *first = v->First();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1389
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1390
	/* make sure the vehicle is stopped in the depot */
3183
90c676e6a50d (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
  1391
	if (CheckTrainStoppedInDepot(first) < 0) {
90c676e6a50d (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
  1392
		return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED);
90c676e6a50d (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3172
diff changeset
  1393
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1394
7526
27c4fbf8aef0 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni
parents: 7502
diff changeset
  1395
	if (IsRearDualheaded(v)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1396
1842
fb9086b04740 (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1397
	if (flags & DC_EXEC) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1398
		if (v == first && IsFrontEngine(first)) {
9274
42b67e65f1c2 (svn r13140) -Codechange: move the gui-list-sorting out of window_gui.h so window_gui.h only needs to be included in *_gui.cpp.
rubidium
parents: 9234
diff changeset
  1399
			DeleteWindowById(WC_VEHICLE_VIEW, first->index);
2618
9691753519ac (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
  1400
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1401
		InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9274
diff changeset
  1402
		InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
1766
8cdb302ae946 (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
	}
8cdb302ae946 (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
  1404
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8226
diff changeset
  1405
	CommandCost cost(EXPENSES_NEW_VEHICLES);
1766
8cdb302ae946 (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
  1406
	switch (p2) {
8cdb302ae946 (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
  1407
		case 0: case 2: { /* Delete given wagon */
8cdb302ae946 (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
  1408
			bool switch_engine = false;    // update second wagon to engine?
8cdb302ae946 (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
  1409
			byte ori_subtype = v->subtype; // backup subtype of deleted wagon in case DeleteVehicle() changes
8cdb302ae946 (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
  1410
8cdb302ae946 (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
  1411
			/* 1. Delete the engine, if it is dualheaded also delete the matching
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1412
			 * rear engine of the loco (from the point of deletion onwards) */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1413
			Vehicle *rear = (IsMultiheaded(v) &&
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1414
				IsTrainEngine(v)) ? v->u.rail.other_multiheaded_part : NULL;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1415
1766
8cdb302ae946 (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
  1416
			if (rear != NULL) {
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6980
diff changeset
  1417
				cost.AddCost(-rear->value);
1766
8cdb302ae946 (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
  1418
				if (flags & DC_EXEC) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1419
					UnlinkWagon(rear, first);
5256
9056fd4b30f4 (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
  1420
					DeleteDepotHighlightOfVehicle(rear);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  1421
					delete rear;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1422
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1423
			}
1766
8cdb302ae946 (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
10192
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1425
			/* 2. We are selling the front vehicle, some special action might be required
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1426
			 * here, so take attention */
10192
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1427
			if (v == first) {
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1428
				Vehicle *new_f = GetNextVehicle(first);
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1429
8cdb302ae946 (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
  1430
				/* 2.2 If there are wagons present after the deleted front engine, check
6322
62c0c7a59e3c (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6316
diff changeset
  1431
				 * if the second wagon (which will be first) is an engine. If it is one,
62c0c7a59e3c (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6316
diff changeset
  1432
				 * promote it as a new train, retaining the unitnumber, orders */
62c0c7a59e3c (svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents: 6316
diff changeset
  1433
				if (new_f != NULL && IsTrainEngine(new_f)) {
10192
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1434
					if (IsTrainEngine(first)) {
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1435
						/* Let the new front engine take over the setup of the old engine */
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1436
						switch_engine = true;
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1437
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1438
						if (flags & DC_EXEC) {
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1439
							/* Make sure the group counts stay correct. */
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1440
							new_f->group_id        = first->group_id;
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1441
							first->group_id        = DEFAULT_GROUP;
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1442
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1443
							/* Copy orders (by sharing) */
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1444
							new_f->orders          = first->orders;
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1445
							new_f->num_orders      = first->num_orders;
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1446
							new_f->AddToShared(first);
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1447
							DeleteVehicleOrders(first);
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1448
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1449
							/* Copy other important data from the front engine */
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1450
							new_f->CopyVehicleConfigAndStatistics(first);
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1451
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1452
							/* If we deleted a window then open a new one for the 'new' train */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
  1453
							if (IsLocalCompany() && w != NULL) ShowVehicleViewWindow(new_f);
10192
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1454
						}
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1455
					} else {
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1456
						/* We are selling a free wagon, and construct a new train at the same time.
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1457
						 * This needs lots of extra checks (e.g. train limit), which are done by first moving
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1458
						 * the remaining vehicles to a new row */
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1459
						cost.AddCost(DoCommand(0, new_f->index | INVALID_VEHICLE << 16, 1, flags, CMD_MOVE_RAIL_VEHICLE));
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1460
						if (cost.Failed()) return cost;
6ef54dd65a97 (svn r14405) -Fix [FS#2289]: Correct handling of selling free wagons while the following vehicle is an engine (i.e. a new train is created).
frosch
parents: 10190
diff changeset
  1461
					}
1766
8cdb302ae946 (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
  1462
				}
8cdb302ae946 (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
			}
8cdb302ae946 (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
  1464
8cdb302ae946 (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
			/* 3. Delete the requested wagon */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1466
			cost.AddCost(-v->value);
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1467
			if (flags & DC_EXEC) {
8cdb302ae946 (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
  1468
				first = UnlinkWagon(v, first);
5256
9056fd4b30f4 (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
  1469
				DeleteDepotHighlightOfVehicle(v);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  1470
				delete v;
1766
8cdb302ae946 (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
  1471
8cdb302ae946 (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
  1472
				/* 4 If the second wagon was an engine, update it to front_engine
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1473
				 * which UnlinkWagon() has changed to TS_Free_Car */
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1474
				if (switch_engine) SetFrontEngine(first);
1766
8cdb302ae946 (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
  1475
8cdb302ae946 (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
  1476
				/* 5. If the train still exists, update its acceleration, window, etc. */
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1477
				if (first != NULL) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1478
					NormaliseTrainConsist(first);
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
  1479
					TrainConsistChanged(first, false);
6643
f81bee57bc09 (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: 6608
diff changeset
  1480
					UpdateTrainGroupID(first);
8706
95102fcf66b4 (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: 8705
diff changeset
  1481
					if (IsFrontEngine(first)) InvalidateWindow(WC_VEHICLE_REFIT, first->index);
1766
8cdb302ae946 (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
  1482
				}
8cdb302ae946 (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
  1483
8cdb302ae946 (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
  1484
8cdb302ae946 (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
  1485
				/* (6.) Borked AI. If it sells an engine it expects all wagons lined
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1486
				 * up on a new line to be added to the newly built loco. Replace it is.
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1487
				 * Totally braindead cause building a new engine adds all loco-less
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1488
				 * engines to its train anyways */
8612
21e2e71f3f4f (svn r12194) -Codechange: apply coding style on enum TrainSubtype
smatz
parents: 8607
diff changeset
  1489
				if (p2 == 2 && HasBit(ori_subtype, TS_FRONT)) {
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1490
					for (v = first; v != NULL;) {
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1491
						Vehicle *tmp = GetNextVehicle(v);
3491
35d747bb5e82 (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
  1492
						DoCommand(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1493
						v = tmp;
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1494
					}
8cdb302ae946 (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
				}
8cdb302ae946 (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
			}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1497
		} break;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1498
		case 1: { /* Delete wagon and all wagons after it given certain criteria */
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1499
			/* Start deleting every vehicle after the selected one
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1500
			 * If we encounter a matching rear-engine to a front-engine
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1501
			 * earlier in the chain (before deletion), leave it alone */
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1502
			for (Vehicle *tmp; v != NULL; v = tmp) {
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1503
				tmp = GetNextVehicle(v);
1766
8cdb302ae946 (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
  1504
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1505
				if (IsMultiheaded(v)) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1506
					if (IsTrainEngine(v)) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1507
						/* We got a front engine of a multiheaded set. Now we will sell the rear end too */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1508
						Vehicle *rear = v->u.rail.other_multiheaded_part;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1509
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1510
						if (rear != NULL) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1511
							cost.AddCost(-rear->value);
6785
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1512
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1513
							/* If this is a multiheaded vehicle with nothing
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1514
							 * between the parts, tmp will be pointing to the
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1515
							 * rear part, which is unlinked from the train and
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1516
							 * deleted here. However, because tmp has already
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1517
							 * been set it needs to be updated now so that the
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1518
							 * loop never sees the rear part. */
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1519
							if (tmp == rear) tmp = GetNextVehicle(tmp);
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1520
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1521
							if (flags & DC_EXEC) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1522
								first = UnlinkWagon(rear, first);
5256
9056fd4b30f4 (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
  1523
								DeleteDepotHighlightOfVehicle(rear);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  1524
								delete rear;
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1525
							}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1526
						}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1527
					} else if (v->u.rail.other_multiheaded_part != NULL) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1528
						/* The front to this engine is earlier in this train. Do nothing */
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1529
						continue;
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1530
					}
1766
8cdb302ae946 (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
  1531
				}
8cdb302ae946 (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
  1532
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1533
				cost.AddCost(-v->value);
1766
8cdb302ae946 (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
  1534
				if (flags & DC_EXEC) {
8cdb302ae946 (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
  1535
					first = UnlinkWagon(v, first);
5256
9056fd4b30f4 (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
  1536
					DeleteDepotHighlightOfVehicle(v);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  1537
					delete v;
1766
8cdb302ae946 (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
  1538
				}
8cdb302ae946 (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
  1539
			}
8cdb302ae946 (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
  1540
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1541
			/* 3. If it is still a valid train after selling, update its acceleration and cached values */
3017
a75caf4efa2d (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
  1542
			if (flags & DC_EXEC && first != NULL) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1543
				NormaliseTrainConsist(first);
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
  1544
				TrainConsistChanged(first, false);
6643
f81bee57bc09 (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: 6608
diff changeset
  1545
				UpdateTrainGroupID(first);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1546
				InvalidateWindow(WC_VEHICLE_REFIT, first->index);
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1547
			}
1766
8cdb302ae946 (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
  1548
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1549
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1550
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1553
void Train::UpdateDeltaXY(Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1554
{
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1555
#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
  1556
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1558
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1559
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1561
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1564
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1565
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1566
#undef MKIT
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1567
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1568
	uint32 x = _delta_xy_table[direction];
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1569
	this->x_offs        = GB(x,  0, 8);
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1570
	this->y_offs        = GB(x,  8, 8);
8793
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
diff changeset
  1571
	this->x_extent      = GB(x, 16, 8);
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
diff changeset
  1572
	this->y_extent      = GB(x, 24, 8);
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
diff changeset
  1573
	this->z_extent      = 6;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1574
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1575
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1576
static void UpdateVarsAfterSwap(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1577
{
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1578
	v->UpdateDeltaXY(v->direction);
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7109
diff changeset
  1579
	v->cur_image = v->GetImage(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1580
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1581
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1582
	EndVehicleMove(v);
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
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1585
static inline void SetLastSpeed(Vehicle *v, int spd)
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1586
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1587
	int old = v->u.rail.last_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1588
	if (spd != old) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
		v->u.rail.last_speed = spd;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  1590
		if (_settings_client.gui.vehicle_speed || (old == 0) != (spd == 0)) {
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8344
diff changeset
  1591
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1592
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1593
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1594
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1595
9808
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1596
/** Mark a train as stuck and stop it if it isn't stopped right now. */
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1597
static void MarkTrainAsStuck(Vehicle *v)
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1598
{
9808
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1599
	if (!HasBit(v->u.rail.flags, VRF_TRAIN_STUCK)) {
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1600
		/* It is the first time the problem occured, set the "train stuck" flag. */
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1601
		SetBit(v->u.rail.flags, VRF_TRAIN_STUCK);
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1602
		v->load_unload_time_rem	= 0;
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1603
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1604
		/* Stop train */
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1605
		v->cur_speed = 0;
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1606
		v->subspeed = 0;
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1607
		SetLastSpeed(v, 0);
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1608
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1609
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1610
	}
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1611
}
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1612
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1613
static void SwapTrainFlags(uint16 *swap_flag1, uint16 *swap_flag2)
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1614
{
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1615
	uint16 flag1 = *swap_flag1;
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  1616
	uint16 flag2 = *swap_flag2;
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1617
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1618
	/* Clear the flags */
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1619
	ClrBit(*swap_flag1, VRF_GOINGUP);
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1620
	ClrBit(*swap_flag1, VRF_GOINGDOWN);
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1621
	ClrBit(*swap_flag2, VRF_GOINGUP);
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1622
	ClrBit(*swap_flag2, VRF_GOINGDOWN);
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1623
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1624
	/* Reverse the rail-flags (if needed) */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1625
	if (HasBit(flag1, VRF_GOINGUP)) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1626
		SetBit(*swap_flag2, VRF_GOINGDOWN);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1627
	} else if (HasBit(flag1, VRF_GOINGDOWN)) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1628
		SetBit(*swap_flag2, VRF_GOINGUP);
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1629
	}
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1630
	if (HasBit(flag2, VRF_GOINGUP)) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1631
		SetBit(*swap_flag1, VRF_GOINGDOWN);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1632
	} else if (HasBit(flag2, VRF_GOINGDOWN)) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1633
		SetBit(*swap_flag1, VRF_GOINGUP);
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1634
	}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1635
}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1636
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1637
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1638
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1639
	Vehicle *a, *b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1640
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1641
	/* locate vehicles to swap */
7492
35acee076719 (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: 7488
diff changeset
  1642
	for (a = v; l != 0; l--) a = a->Next();
35acee076719 (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: 7488
diff changeset
  1643
	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
  1644
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
	if (a != b) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1646
		/* swap the hidden bits */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1647
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1648
			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
  1649
			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
  1650
			a->vehstatus = tmp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1651
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1652
5733
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1653
		Swap(a->u.rail.track, b->u.rail.track);
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1654
		Swap(a->direction,    b->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1655
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1656
		/* toggle direction */
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1657
		if (a->u.rail.track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction);
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1658
		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
  1659
5733
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1660
		Swap(a->x_pos, b->x_pos);
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1661
		Swap(a->y_pos, b->y_pos);
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1662
		Swap(a->tile,  b->tile);
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1663
		Swap(a->z_pos, b->z_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1664
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1665
		SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1666
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1667
		/* update other vars */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1668
		UpdateVarsAfterSwap(a);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1669
		UpdateVarsAfterSwap(b);
1554
79809a004e97 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1670
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  1671
		/* call the proper EnterTile function unless we are in a wormhole */
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1672
		if (a->u.rail.track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1673
		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
  1674
	} else {
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1675
		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
  1676
		UpdateVarsAfterSwap(a);
1554
79809a004e97 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1677
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1678
		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
  1679
	}
3355
e414a0b104a6 (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
  1680
e414a0b104a6 (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
  1681
	/* Update train's power incase tiles were different rail type */
e414a0b104a6 (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
  1682
	TrainPowerChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1683
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1684
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1685
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1686
/**
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1687
 * Check if the vehicle is a train
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1688
 * @param v vehicle on tile
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1689
 * @return v if it is a train, NULL otherwise
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1690
 */
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9774
diff changeset
  1691
static Vehicle *TrainOnTileEnum(Vehicle *v, void *)
744
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1692
{
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1693
	return (v->type == VEH_TRAIN) ? v : NULL;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1694
}
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1695
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1696
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1697
/**
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1698
 * Checks if a train is approaching a rail-road crossing
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1699
 * @param v vehicle on tile
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1700
 * @param data tile with crossing we are testing
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1701
 * @return v if it is approaching a crossing, NULL otherwise
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1702
 */
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9774
diff changeset
  1703
static Vehicle *TrainApproachingCrossingEnum(Vehicle *v, void *data)
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1704
{
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1705
	/* not a train || not front engine || crashed */
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1706
	if (v->type != VEH_TRAIN || !IsFrontEngine(v) || v->vehstatus & VS_CRASHED) return NULL;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1707
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1708
	TileIndex tile = *(TileIndex*)data;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1709
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1710
	if (TrainApproachingCrossingTile(v) != tile) return NULL;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1711
744
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1712
	return v;
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1713
}
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1714
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1715
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1716
/**
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1717
 * Finds a vehicle approaching rail-road crossing
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1718
 * @param tile tile to test
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  1719
 * @return true if a vehicle is approaching the crossing
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1720
 * @pre tile is a rail-road crossing
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1721
 */
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  1722
static bool TrainApproachingCrossing(TileIndex tile)
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1723
{
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1724
	assert(IsLevelCrossingTile(tile));
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1725
8598
ddd1f342b0da (svn r12179) -Codechange: use GetCrossingRailTrack() and GetCrossingRailAxis() to improve code readability
smatz
parents: 8597
diff changeset
  1726
	DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile));
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1727
	TileIndex tile_from = tile + TileOffsByDiagDir(dir);
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1728
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  1729
	if (HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum)) return true;
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1730
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1731
	dir = ReverseDiagDir(dir);
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1732
	tile_from = tile + TileOffsByDiagDir(dir);
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1733
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  1734
	return HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum);
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1735
}
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1736
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1737
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1738
/**
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1739
 * Sets correct crossing state
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1740
 * @param tile tile to update
8344
1cd2a831b06c (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8342
diff changeset
  1741
 * @param sound should we play sound?
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1742
 * @pre tile is a rail-road crossing
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1743
 */
8344
1cd2a831b06c (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8342
diff changeset
  1744
void UpdateLevelCrossing(TileIndex tile, bool sound)
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1745
{
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1746
	assert(IsLevelCrossingTile(tile));
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1747
9824
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9822
diff changeset
  1748
	/* train on crossing || train approaching crossing || reserved */
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  1749
	bool new_state = HasVehicleOnPos(tile, NULL, &TrainOnTileEnum) || TrainApproachingCrossing(tile) || GetCrossingReservation(tile);
8344
1cd2a831b06c (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8342
diff changeset
  1750
1cd2a831b06c (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8342
diff changeset
  1751
	if (new_state != IsCrossingBarred(tile)) {
1cd2a831b06c (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8342
diff changeset
  1752
		if (new_state && sound) {
1cd2a831b06c (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8342
diff changeset
  1753
			SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
1cd2a831b06c (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8342
diff changeset
  1754
		}
1cd2a831b06c (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8342
diff changeset
  1755
		SetCrossingBarred(tile, new_state);
1cd2a831b06c (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz
parents: 8342
diff changeset
  1756
		MarkTileDirtyByTile(tile);
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1757
	}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1758
}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1759
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1760
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1761
/**
8356
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1762
 * Bars crossing and plays ding-ding sound if not barred already
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1763
 * @param tile tile with crossing
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1764
 * @pre tile is a rail-road crossing
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1765
 */
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1766
static inline void MaybeBarCrossingWithSound(TileIndex tile)
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1767
{
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1768
	if (!IsCrossingBarred(tile)) {
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1769
		BarCrossing(tile);
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1770
		SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1771
		MarkTileDirtyByTile(tile);
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1772
	}
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1773
}
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1774
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1775
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1776
/**
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1777
 * Advances wagons for train reversing, needed for variable length wagons.
8712
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1778
 * This one is called before the train is reversed.
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1779
 * @param v First vehicle in chain
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1780
 */
8712
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1781
static void AdvanceWagonsBeforeSwap(Vehicle *v)
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1782
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1783
	Vehicle *base = v;
8712
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1784
	Vehicle *first = base;                    // first vehicle to move
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1785
	Vehicle *last = GetLastVehicleInChain(v); // last vehicle to move
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1786
	uint length = CountVehiclesInChain(v);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1787
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1788
	while (length > 2) {
8712
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1789
		last = last->Previous();
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1790
		first = first->Next();
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1791
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1792
		int differential = base->u.rail.cached_veh_length - last->u.rail.cached_veh_length;
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1793
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1794
		/* do not update images now
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1795
		 * negative differential will be handled in AdvanceWagonsAfterSwap() */
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1796
		for (int i = 0; i < differential; i++) TrainController(first, last->Next(), false);
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1797
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1798
		base = first; // == base->Next()
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1799
		length -= 2;
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1800
	}
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1801
}
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1802
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1803
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1804
/**
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1805
 * Advances wagons for train reversing, needed for variable length wagons.
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1806
 * This one is called after the train is reversed.
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1807
 * @param v First vehicle in chain
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1808
 */
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1809
static void AdvanceWagonsAfterSwap(Vehicle *v)
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1810
{
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1811
	/* first of all, fix the situation when the train was entering a depot */
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1812
	Vehicle *dep = v; // last vehicle in front of just left depot
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1813
	while (dep->Next() != NULL && (dep->u.rail.track == TRACK_BIT_DEPOT || dep->Next()->u.rail.track != TRACK_BIT_DEPOT)) {
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1814
		dep = dep->Next(); // find first vehicle outside of a depot, with next vehicle inside a depot
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1815
	}
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1816
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1817
	Vehicle *leave = dep->Next(); // first vehicle in a depot we are leaving now
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1818
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1819
	if (leave != NULL) {
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1820
		/* 'pull' next wagon out of the depot, so we won't miss it (it could stay in depot forever) */
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1821
		int d = TicksToLeaveDepot(dep);
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1822
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1823
		if (d <= 0) {
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1824
			leave->vehstatus &= ~VS_HIDDEN; // move it out of the depot
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9222
diff changeset
  1825
			leave->u.rail.track = TrackToTrackBits(GetRailDepotTrack(leave->tile));
8712
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1826
			for (int i = 0; i >= d; i--) TrainController(leave, NULL, false); // maybe move it, and maybe let another wagon leave
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1827
		}
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1828
	} else {
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1829
		dep = NULL; // no vehicle in a depot, so no vehicle leaving a depot
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1830
	}
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1831
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1832
	Vehicle *base = v;
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1833
	Vehicle *first = base;                    // first vehicle to move
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1834
	Vehicle *last = GetLastVehicleInChain(v); // last vehicle to move
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1835
	uint length = CountVehiclesInChain(v);
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1836
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1837
	/* we have to make sure all wagons that leave a depot because of train reversing are moved coorectly
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1838
	 * they have already correct spacing, so we have to make sure they are moved how they should */
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1839
	bool nomove = (dep == NULL); // if there is no vehicle leaving a depot, limit the number of wagons moved immediatelly
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1840
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1841
	while (length > 2) {
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1842
		/* we reached vehicle (originally) in front of a depot, stop now
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1843
		 * (we would move wagons that are alredy moved with new wagon length) */
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1844
		if (base == dep) break;
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1845
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1846
		/* the last wagon was that one leaving a depot, so do not move it anymore */
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1847
		if (last == dep) nomove = true;
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1848
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1849
		last = last->Previous();
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1850
		first = first->Next();
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1851
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1852
		int differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length;
8712
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1853
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1854
		/* do not update images now */
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1855
		for (int i = 0; i < differential; i++) TrainController(first, (nomove ? last->Next() : NULL), false);
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1856
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1857
		base = first; // == base->Next()
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1858
		length -= 2;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1859
	}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1860
}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1861
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2928
diff changeset
  1862
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1863
static void ReverseTrainDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1864
{
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
  1865
	if (IsRailDepotTile(v->tile)) {
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1866
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1867
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1868
9819
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1869
	/* Clear path reservation in front. */
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1870
	FreeTrainTrackReservation(v);
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1871
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1872
	/* Check if we were approaching a rail/road-crossing */
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1873
	TileIndex crossing = TrainApproachingCrossingTile(v);
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1874
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1875
	/* count number of vehicles */
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1876
	int r = CountVehiclesInChain(v) - 1;  // number of vehicles - 1
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1877
8712
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1878
	AdvanceWagonsBeforeSwap(v);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1879
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1880
	/* swap start<>end, start+1<>end-1, ... */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1881
	int l = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1882
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1883
		ReverseTrainSwapVeh(v, l++, r--);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1884
	} while (l <= r);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1885
8712
be50475ea8fc (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz
parents: 8710
diff changeset
  1886
	AdvanceWagonsAfterSwap(v);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1887
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
  1888
	if (IsRailDepotTile(v->tile)) {
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1889
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1890
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1891
8938
9147361fb2be (svn r12711) -Codechange: remove unneeded loops
glx
parents: 8928
diff changeset
  1892
	ToggleBit(v->u.rail.flags, VRF_TOGGLE_REVERSE);
8143
4627396b4811 (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: 8140
diff changeset
  1893
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1894
	ClrBit(v->u.rail.flags, VRF_REVERSING);
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1895
8667
e25ed88a426c (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 8666
diff changeset
  1896
	/* recalculate cached data */
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
  1897
	TrainConsistChanged(v, true);
8667
e25ed88a426c (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 8666
diff changeset
  1898
e25ed88a426c (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 8666
diff changeset
  1899
	/* update all images */
e25ed88a426c (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 8666
diff changeset
  1900
	for (Vehicle *u = v; u != NULL; u = u->Next()) u->cur_image = u->GetImage(u->direction);
e25ed88a426c (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138
parents: 8666
diff changeset
  1901
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1902
	/* update crossing we were approaching */
8342
bf710180e7ce (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8334
diff changeset
  1903
	if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1904
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1905
	/* maybe we are approaching crossing now, after reversal */
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  1906
	crossing = TrainApproachingCrossingTile(v);
8356
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  1907
	if (crossing != INVALID_TILE) MaybeBarCrossingWithSound(crossing);
9819
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1908
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1909
	/* If we are inside a depot after reversing, don't bother with path reserving. */
9973
6ec0621a124d (svn r14130) -Fix (r13961) [FS#2248]: Clear the stuck state of a train that is reversing with the first vehicle inside a depot. (michi_cc)
frosch
parents: 9941
diff changeset
  1910
	if (v->u.rail.track & TRACK_BIT_DEPOT) {
6ec0621a124d (svn r14130) -Fix (r13961) [FS#2248]: Clear the stuck state of a train that is reversing with the first vehicle inside a depot. (michi_cc)
frosch
parents: 9941
diff changeset
  1911
		/* Can't be stuck here as inside a depot is always a safe tile. */
6ec0621a124d (svn r14130) -Fix (r13961) [FS#2248]: Clear the stuck state of a train that is reversing with the first vehicle inside a depot. (michi_cc)
frosch
parents: 9941
diff changeset
  1912
		if (HasBit(v->u.rail.flags, VRF_TRAIN_STUCK)) InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
6ec0621a124d (svn r14130) -Fix (r13961) [FS#2248]: Clear the stuck state of a train that is reversing with the first vehicle inside a depot. (michi_cc)
frosch
parents: 9941
diff changeset
  1913
		ClrBit(v->u.rail.flags, VRF_TRAIN_STUCK);
6ec0621a124d (svn r14130) -Fix (r13961) [FS#2248]: Clear the stuck state of a train that is reversing with the first vehicle inside a depot. (michi_cc)
frosch
parents: 9941
diff changeset
  1914
		return;
6ec0621a124d (svn r14130) -Fix (r13961) [FS#2248]: Clear the stuck state of a train that is reversing with the first vehicle inside a depot. (michi_cc)
frosch
parents: 9941
diff changeset
  1915
	}
9819
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1916
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1917
	/* TrainExitDir does not always produce the desired dir for depots and
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1918
	 * tunnels/bridges that is needed for UpdateSignalsOnSegment. */
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1919
	DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track);
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1920
	if (IsRailDepotTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE)) dir = INVALID_DIAGDIR;
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1921
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1922
	if (UpdateSignalsOnSegment(v->tile, dir, v->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) {
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1923
		/* If we are currently on a tile with conventional signals, we can't treat the
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1924
		 * current tile as a safe tile or we would enter a PBS block without a reservation. */
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1925
		bool first_tile_okay = !(IsTileType(v->tile, MP_RAILWAY) &&
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1926
			HasSignalOnTrackdir(v->tile, GetVehicleTrackdir(v)) &&
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1927
			!IsPbsSignal(GetSignalType(v->tile, FindFirstTrack(v->u.rail.track))));
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1928
9874
6d3b42eab489 (svn r14021) -Fix (r13957) [YAPP]: Overbuilding station tiles with non-track tiles could produce stale reservations. (michi_cc)
smatz
parents: 9857
diff changeset
  1929
		if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(GetVehicleTrackdir(v)), true);
9891
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9888
diff changeset
  1930
		if (TryPathReserve(v, false, first_tile_okay)) {
9819
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1931
			/* Do a look-ahead now in case our current tile was already a safe tile. */
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1932
			CheckNextTrainTile(v);
9891
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9888
diff changeset
  1933
		} else if (v->current_order.GetType() != OT_LOADING) {
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9888
diff changeset
  1934
			/* Do not wait for a way out when we're still loading */
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9888
diff changeset
  1935
			MarkTrainAsStuck(v);
9819
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1936
		}
e5c938adf4f3 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium
parents: 9818
diff changeset
  1937
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1938
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1939
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1940
/** Reverse train.
3491
35d747bb5e82 (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
  1941
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1942
 * @param flags type of operation
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1943
 * @param p1 train to reverse
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1944
 * @param p2 if true, reverse a unit in a train (needs to be in a depot)
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1945
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1946
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
  1947
{
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1948
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1949
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1950
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1951
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1952
	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
  1953
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  1954
	if (p2 != 0) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1955
		/* turn a single unit around */
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1956
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1957
		if (IsMultiheaded(v) || HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) {
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1958
			return_cmd_error(STR_ONLY_TURN_SINGLE_UNIT);
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1959
		}
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1960
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1961
		Vehicle *front = v->First();
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1962
		/* make sure the vehicle is stopped in the depot */
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1963
		if (CheckTrainStoppedInDepot(front) < 0) {
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1964
			return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED);
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1965
		}
3257
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1966
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1967
		if (flags & DC_EXEC) {
7932
6c3d71e8a129 (svn r11485) -Codechange: Remove the doubled function ToggleBitT and rename the remaining to fit with the naming style
skidd13
parents: 7931
diff changeset
  1968
			ToggleBit(v->u.rail.flags, VRF_REVERSE_DIRECTION);
4856
f1777f3659d8 (svn r6782) - Fix (r3947): Invalidate depot & vehicle windows when reversing a single engine with ctrl-click.
peter1138
parents: 4839
diff changeset
  1969
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
f1777f3659d8 (svn r6782) - Fix (r3947): Invalidate depot & vehicle windows when reversing a single engine with ctrl-click.
peter1138
parents: 4839
diff changeset
  1970
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
3257
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1971
		}
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1972
	} else {
7476
f6eb56e8b8a7 (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: 7425
diff changeset
  1973
		/* turn the whole train around */
f6eb56e8b8a7 (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: 7425
diff changeset
  1974
		if (v->vehstatus & VS_CRASHED || v->breakdown_ctr != 0) return CMD_ERROR;
3257
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1975
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1976
		if (flags & DC_EXEC) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  1977
			if (_settings_game.vehicle.realistic_acceleration && v->cur_speed != 0) {
7932
6c3d71e8a129 (svn r11485) -Codechange: Remove the doubled function ToggleBitT and rename the remaining to fit with the naming style
skidd13
parents: 7931
diff changeset
  1978
				ToggleBit(v->u.rail.flags, VRF_REVERSING);
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1979
			} else {
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1980
				v->cur_speed = 0;
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1981
				SetLastSpeed(v, 0);
10176
bd20cc54b41f (svn r14371) -Fix [FS#2313]: loading indicator didn't stay with the front engine when turning a train in a station.
rubidium
parents: 10173
diff changeset
  1982
				HideFillingPercent(&v->fill_percent_te_id);
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1983
				ReverseTrainDirection(v);
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1984
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1985
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1986
	}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1987
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1988
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1989
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1990
/** Force a train through a red signal
3491
35d747bb5e82 (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
  1991
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1992
 * @param flags type of operation
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1993
 * @param p1 train to ignore the red signal
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1994
 * @param p2 unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1995
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1996
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
  1997
{
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1998
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1999
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2000
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2001
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  2002
	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
  2003
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2004
	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
  2005
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  2006
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2007
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2008
1802
da61740cc1e7 (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
  2009
/** Refits a train to the specified cargo type.
3491
35d747bb5e82 (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
  2010
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2011
 * @param flags type of operation
1802
da61740cc1e7 (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
  2012
 * @param p1 vehicle ID of the train to refit
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  2013
 * param p2 various bitstuffed elements
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  2014
 * - p2 = (bit 0-7) - the new cargo type to refit to
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  2015
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6502
diff changeset
  2016
 * - p2 = (bit 16) - refit only this vehicle
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6502
diff changeset
  2017
 * @return cost of refit or error
1802
da61740cc1e7 (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
  2018
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  2019
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
  2020
{
2635
7ed07303448d (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2634
diff changeset
  2021
	CargoID new_cid = GB(p2, 0, 8);
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  2022
	byte new_subtype = GB(p2, 8, 8);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2023
	bool only_this = HasBit(p2, 16);
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  2024
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  2025
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  2026
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2027
	Vehicle *v = GetVehicle(p1);
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  2028
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  2029
	if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR;
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
  2030
	if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
8145
2116aa2c6263 (svn r11707) -Fix: do not allow refitting flooded (destroyed) vehicles
smatz
parents: 8144
diff changeset
  2031
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
1802
da61740cc1e7 (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
  2032
da61740cc1e7 (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
  2033
	/* Check cargo */
6316
d2b70e10f106 (svn r9259) -Codechange: NUM_CARGO isn't a valid cargo type...
peter1138
parents: 6303
diff changeset
  2034
	if (new_cid >= NUM_CARGO) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2035
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8226
diff changeset
  2036
	CommandCost cost(EXPENSES_TRAIN_RUN);
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2037
	uint num = 0;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  2038
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2039
	do {
491
6527219352cd (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
  2040
		/* XXX: We also refit all the attached wagons en-masse if they
6527219352cd (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
  2041
		 * can be refitted. This is how TTDPatch does it.  TODO: Have
6527219352cd (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
  2042
		 * some nice [Refit] button near each wagon. --pasky */
2704
bdf6ae0cb27c (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
  2043
		if (!CanRefitTo(v->engine_type, new_cid)) continue;
1802
da61740cc1e7 (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
  2044
1859
870dcb6fd65b (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
  2045
		if (v->cargo_cap != 0) {
1895
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  2046
			uint16 amount = CALLBACK_FAILED;
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  2047
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2048
			if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
3988
fcb1549e4d4c (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  2049
				/* Back up the vehicle's cargo type */
1895
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  2050
				CargoID temp_cid = v->cargo_type;
3988
fcb1549e4d4c (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  2051
				byte temp_subtype = v->cargo_subtype;
1895
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  2052
				v->cargo_type = new_cid;
3988
fcb1549e4d4c (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  2053
				v->cargo_subtype = new_subtype;
fcb1549e4d4c (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  2054
				/* Check the refit capacity callback */
3390
ae4b0872dc78 (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
  2055
				amount = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
3988
fcb1549e4d4c (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  2056
				/* Restore the original cargo type */
1895
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  2057
				v->cargo_type = temp_cid;
3988
fcb1549e4d4c (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3986
diff changeset
  2058
				v->cargo_subtype = temp_subtype;
1895
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  2059
			}
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  2060
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  2061
			if (amount == CALLBACK_FAILED) { // callback failed or not used, use default
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2062
				const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2063
				CargoID old_cid = rvi->cargo_type;
3017
a75caf4efa2d (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
  2064
				/* normally, the capacity depends on the cargo type, a rail vehicle can
a75caf4efa2d (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
  2065
				 * carry twice as much mail/goods as normal cargo, and four times as
a75caf4efa2d (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
  2066
				 * many passengers
a75caf4efa2d (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
  2067
				 */
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2068
				amount = rvi->capacity;
3017
a75caf4efa2d (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
  2069
				switch (old_cid) {
a75caf4efa2d (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
  2070
					case CT_PASSENGERS: break;
a75caf4efa2d (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
  2071
					case CT_MAIL:
a75caf4efa2d (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
  2072
					case CT_GOODS: amount *= 2; break;
a75caf4efa2d (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
  2073
					default:       amount *= 4; break;
a75caf4efa2d (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
  2074
				}
a75caf4efa2d (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
  2075
				switch (new_cid) {
a75caf4efa2d (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
  2076
					case CT_PASSENGERS: break;
a75caf4efa2d (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
  2077
					case CT_MAIL:
a75caf4efa2d (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
  2078
					case CT_GOODS: amount /= 2; break;
a75caf4efa2d (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
  2079
					default:       amount /= 4; break;
a75caf4efa2d (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
  2080
				}
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2081
			}
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  2082
10328
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2083
			if (new_cid != v->cargo_type) {
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2084
				cost.AddCost(GetRefitCost(v->engine_type));
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2085
			}
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2086
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2087
			num += amount;
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2088
			if (flags & DC_EXEC) {
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2089
				v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2090
				v->cargo_type = new_cid;
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2091
				v->cargo_cap = amount;
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2092
				v->cargo_subtype = new_subtype;
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2093
				InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2094
				InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
7500e2e4c8ab (svn r14578) -Fix (r2389, r10811)[FS#Vikthor]: Allow capacity callbacks (15, 36) to return zero capacity.
frosch
parents: 10314
diff changeset
  2095
				InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2096
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2097
		}
7492
35acee076719 (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: 7488
diff changeset
  2098
	} while ((v = v->Next()) != NULL && !only_this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2099
3008
cef26520c91e (svn r3588) - Refit: Use only one global to return the refit capacity rather than one per vehicle type.
peter1138
parents: 3007
diff changeset
  2100
	_returned_refit_capacity = num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2101
4708
b2d7c0945664 (svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents: 4656
diff changeset
  2102
	/* Update the train's cached variables */
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
  2103
	if (flags & DC_EXEC) TrainConsistChanged(GetVehicle(p1)->First(), false);
4708
b2d7c0945664 (svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents: 4656
diff changeset
  2104
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2105
	return cost;
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
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  2108
struct TrainFindDepotData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2109
	uint best_length;
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  2110
	TileIndex tile;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
  2111
	Owner owner;
1777
f703cf05b5b9 (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
  2112
	/**
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2113
	 * true if reversing is necessary for the train to get to this depot
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2114
	 * This value is unused when new depot finding and NPF are both disabled
1777
f703cf05b5b9 (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
  2115
	 */
f703cf05b5b9 (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
  2116
	bool reverse;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  2117
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2118
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2119
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
  2120
{
3269
62fb247bf94b (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2121
	if (IsTileType(tile, MP_RAILWAY) &&
62fb247bf94b (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2122
			IsTileOwner(tile, tfdd->owner) &&
4182
48dba107ff43 (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
  2123
			IsRailDepot(tile)) {
4406
cc20171473bb (svn r6159) -Fix: FindClosestTrainDepot hardly ever found a depot with NPF off due to absence of distance-normalization (Rojer)
Darkvater
parents: 4389
diff changeset
  2124
		/* approximate number of tiles by dividing by DIAG_FACTOR */
cc20171473bb (svn r6159) -Fix: FindClosestTrainDepot hardly ever found a depot with NPF off due to absence of distance-normalization (Rojer)
Darkvater
parents: 4389
diff changeset
  2125
		tfdd->best_length = length / DIAG_FACTOR;
3269
62fb247bf94b (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2126
		tfdd->tile = tile;
62fb247bf94b (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2127
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2128
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2129
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2130
	return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2131
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2132
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2133
/** returns the tile of a depot to goto to. The given vehicle must not be
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2134
 * crashed! */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2135
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v, int max_distance)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2136
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2137
	assert(!(v->vehstatus & VS_CRASHED));
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2138
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2139
	TrainFindDepotData tfdd;
308
8088f72d10f5 (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
  2140
	tfdd.owner = v->owner;
1777
f703cf05b5b9 (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
  2141
	tfdd.reverse = false;
308
8088f72d10f5 (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
  2142
9934
232a55f10cba (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium
parents: 9928
diff changeset
  2143
	if (IsRailDepotTile(v->tile)) {
232a55f10cba (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium
parents: 9928
diff changeset
  2144
		tfdd.tile = v->tile;
232a55f10cba (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium
parents: 9928
diff changeset
  2145
		tfdd.best_length = 0;
232a55f10cba (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium
parents: 9928
diff changeset
  2146
		return tfdd;
232a55f10cba (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium
parents: 9928
diff changeset
  2147
	}
232a55f10cba (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium
parents: 9928
diff changeset
  2148
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2149
	PBSTileInfo origin = FollowTrainReservation(v);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2150
	if (IsRailDepotTile(origin.tile)) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2151
		tfdd.tile = origin.tile;
308
8088f72d10f5 (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
  2152
		tfdd.best_length = 0;
8088f72d10f5 (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
  2153
		return tfdd;
8088f72d10f5 (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
  2154
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2155
9934
232a55f10cba (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium
parents: 9928
diff changeset
  2156
	tfdd.best_length = UINT_MAX;
232a55f10cba (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium
parents: 9928
diff changeset
  2157
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2158
	uint8 pathfinder = _settings_game.pf.pathfinder_for_trains;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2159
	if ((_settings_game.pf.reserve_paths || HasReservedTracks(v->tile, v->u.rail.track)) && pathfinder == VPF_NTP) pathfinder = VPF_NPF;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2160
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2161
	switch (pathfinder) {
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2162
		case VPF_YAPF: { /* YAPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2163
			bool found = YapfFindNearestRailDepotTwoWay(v, max_distance, NPF_INFINITE_PENALTY, &tfdd.tile, &tfdd.reverse);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2164
			tfdd.best_length = found ? max_distance / 2 : UINT_MAX; // some fake distance or NOT_FOUND
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2165
		} break;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2166
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2167
		case VPF_NPF: { /* NPF */
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2168
			const Vehicle *last = GetLastVehicleInChain(v);
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2169
			Trackdir trackdir = GetVehicleTrackdir(v);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2170
			Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2171
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2172
			assert(trackdir != INVALID_TRACKDIR);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2173
			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);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2174
			if (ftd.best_bird_dist == 0) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2175
				/* Found target */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2176
				tfdd.tile = ftd.node.tile;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2177
				/* Our caller expects a number of tiles, so we just approximate that
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2178
				 * number by this. It might not be completely what we want, but it will
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2179
				 * work for now :-) We can possibly change this when the old pathfinder
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2180
				 * is removed. */
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2181
				tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2182
				if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) tfdd.reverse = true;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2183
			}
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2184
		} break;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2185
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2186
		default:
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2187
		case VPF_NTP: { /* NTP */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2188
			/* search in the forward direction first. */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2189
			DiagDirection i = TrainExitDir(v->direction, v->u.rail.track);
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2190
			NewTrainPathfind(v->tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2191
			if (tfdd.best_length == UINT_MAX){
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2192
				tfdd.reverse = true;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2193
				/* search in backwards direction */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2194
				i = TrainExitDir(ReverseDir(v->direction), v->u.rail.track);
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2195
				NewTrainPathfind(v->tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2196
			}
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2197
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2198
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2199
308
8088f72d10f5 (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
  2200
	return tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2201
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2202
8890
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2203
bool Train::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2204
{
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2205
	TrainFindDepotData tfdd = FindClosestTrainDepot(this, 0);
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10214
diff changeset
  2206
	if (tfdd.best_length == UINT_MAX) return false;
8890
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2207
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2208
	if (location    != NULL) *location    = tfdd.tile;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2209
	if (destination != NULL) *destination = GetDepotByTile(tfdd.tile)->index;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2210
	if (reverse     != NULL) *reverse     = tfdd.reverse;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2211
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2212
	return true;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2213
}
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
  2214
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  2215
/** Send a train to a depot
3491
35d747bb5e82 (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
  2216
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2217
 * @param flags type of operation
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  2218
 * @param p1 train to send to the depot
4451
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
  2219
 * @param p2 various bitmasked elements
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2220
 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2221
 * - p2 bit 8-10 - VLW flag (for mass goto depot)
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  2222
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  2223
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
  2224
{
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2225
	if (p2 & DEPOT_MASS_SEND) {
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2226
		/* Mass goto depot requested */
4546
507b7d9bd834 (svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents: 4544
diff changeset
  2227
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
  2228
		return SendAllVehiclesToDepot(VEH_TRAIN, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
4463
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  2229
	}
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  2230
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  2231
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  2232
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2233
	Vehicle *v = GetVehicle(p1);
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  2234
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8890
diff changeset
  2235
	if (v->type != VEH_TRAIN) return CMD_ERROR;
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8890
diff changeset
  2236
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8890
diff changeset
  2237
	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2238
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2239
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2240
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  2241
void OnTick_Train()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2242
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2243
	_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
  2244
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2245
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2246
static const int8 _vehicle_smoke_pos[8] = {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2247
	1, 1, 1, 0, -1, -1, -1, 0
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2248
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2249
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2250
static void HandleLocomotiveSmokeCloud(const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2251
{
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2252
	bool sound = false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2253
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2254
	if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2255
		return;
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2256
	}
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2257
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2258
	const Vehicle *u = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2259
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2260
	do {
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
  2261
		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2262
		int effect_offset = GB(v->u.rail.cached_vis_effect, 0, 4) - 8;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2263
		byte effect_type = GB(v->u.rail.cached_vis_effect, 4, 2);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2264
		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
  2265
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2266
		/* no smoke? */
5868
94430141c189 (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: 5858
diff changeset
  2267
		if ((rvi->railveh_type == RAILVEH_WAGON && effect_type == 0) ||
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2268
				disable_effect ||
3546
1bfb9cda57ab (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
  2269
				v->vehstatus & VS_HIDDEN) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2270
			continue;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2271
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2272
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2273
		/* No smoke in depots or tunnels */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
  2274
		if (IsRailDepotTile(v->tile) || IsTunnelTile(v->tile)) continue;
3590
215fc77ee7c6 (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
  2275
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2276
		/* No sparks for electric vehicles on nonelectrified tracks */
6154
8f4c65bdf930 (svn r8899) -Fix
tron
parents: 6153
diff changeset
  2277
		if (!HasPowerOnRail(v->u.rail.railtype, GetTileRailType(v->tile))) continue;
2612
eccc835aaa44 (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  2278
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2279
		if (effect_type == 0) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2280
			/* Use default effect type for engine class. */
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
  2281
			effect_type = rvi->engclass;
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2282
		} else {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2283
			effect_type--;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2284
		}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2285
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2286
		int x = _vehicle_smoke_pos[v->direction] * effect_offset;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2287
		int y = _vehicle_smoke_pos[(v->direction + 2) % 8] * effect_offset;
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2288
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2289
		if (HasBit(v->u.rail.flags, VRF_REVERSE_DIRECTION)) {
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2290
			x = -x;
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2291
			y = -y;
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2292
		}
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2293
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2294
		switch (effect_type) {
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2295
			case 0:
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2296
				/* steam smoke. */
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2297
				if (GB(v->tick_counter, 0, 4) == 0) {
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2298
					CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE);
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2299
					sound = true;
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2300
				}
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2301
				break;
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2302
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2303
			case 1:
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2304
				/* diesel smoke */
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2305
				if (u->cur_speed <= 40 && Chance16(15, 128)) {
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2306
					CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2307
					sound = true;
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2308
				}
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2309
				break;
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2310
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2311
			case 2:
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2312
				/* blue spark */
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2313
				if (GB(v->tick_counter, 0, 2) == 0 && Chance16(1, 45)) {
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2314
					CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2315
					sound = true;
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2316
				}
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2317
				break;
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2318
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2319
			default:
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2320
				break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2321
		}
7492
35acee076719 (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: 7488
diff changeset
  2322
	} while ((v = v->Next()) != NULL);
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2323
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2324
	if (sound) PlayVehicleSound(u, VSE_TRAIN_EFFECT);
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
8550
9f90ede990ac (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 8510
diff changeset
  2327
void Train::PlayLeaveStationSound() const
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2328
{
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2329
	static const SoundFx sfx[] = {
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2330
		SND_04_TRAIN,
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2331
		SND_0A_TRAIN_HORN,
6586
7491de82cefd (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium
parents: 6580
diff changeset
  2332
		SND_0A_TRAIN_HORN,
7491de82cefd (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium
parents: 6580
diff changeset
  2333
		SND_47_MAGLEV_2,
7491de82cefd (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium
parents: 6580
diff changeset
  2334
		SND_41_MAGLEV
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2335
	};
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2336
8550
9f90ede990ac (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 8510
diff changeset
  2337
	if (PlayVehicleSound(this, VSE_START)) return;
9f90ede990ac (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 8510
diff changeset
  2338
9f90ede990ac (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 8510
diff changeset
  2339
	EngineID engtype = this->engine_type;
9f90ede990ac (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 8510
diff changeset
  2340
	SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], this);
6593
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
  2341
}
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
  2342
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2343
/** Check if the train is on the last reserved tile and try to extend the path then. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2344
static void CheckNextTrainTile(Vehicle *v)
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2345
{
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2346
	/* Don't do any look-ahead if path_backoff_interval is 255. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2347
	if (_settings_game.pf.path_backoff_interval == 255) return;
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2348
10102
dcd66d9d53af (svn r14284) -Fix (r13958): Trains would not look ahead and extend their reservation on a waypoint tile in all cases.
michi_cc
parents: 10083
diff changeset
  2349
	/* Exit if we reached our destination depot or are inside a depot. */
dcd66d9d53af (svn r14284) -Fix (r13958): Trains would not look ahead and extend their reservation on a waypoint tile in all cases.
michi_cc
parents: 10083
diff changeset
  2350
	if ((v->tile == v->dest_tile && v->current_order.IsType(OT_GOTO_DEPOT)) || v->u.rail.track & TRACK_BIT_DEPOT) return;
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2351
	/* Exit if we are on a station tile and are going to stop. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2352
	if (IsRailwayStationTile(v->tile) && v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile))) return;
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2353
	/* Exit if the current order doesn't have a destination, but the train has orders. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2354
	if ((v->current_order.IsType(OT_NOTHING) || v->current_order.IsType(OT_LEAVESTATION)) && v->num_orders > 0) return;
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2355
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2356
	Trackdir td = GetVehicleTrackdir(v);
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2357
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2358
	/* On a tile with a red non-pbs signal, don't look ahead. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2359
	if (IsTileType(v->tile, MP_RAILWAY) && HasSignalOnTrackdir(v->tile, td) &&
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2360
			!IsPbsSignal(GetSignalType(v->tile, TrackdirToTrack(td))) &&
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2361
			GetSignalStateByTrackdir(v->tile, td) == SIGNAL_STATE_RED) return;
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2362
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2363
	CFollowTrackRail ft(v);
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2364
	if (!ft.Follow(v->tile, td)) return;
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2365
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2366
	if (!HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(ft.m_new_td_bits))) {
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2367
		/* Next tile is not reserved. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2368
		if (KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2369
			if (HasPbsSignalOnTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) {
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2370
				/* If the next tile is a PBS signal, try to make a reservation. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2371
				TrackBits tracks = TrackdirBitsToTrackBits(ft.m_new_td_bits);
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2372
				if (_settings_game.pf.pathfinder_for_trains != VPF_NTP && _settings_game.pf.forbid_90_deg) {
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2373
					tracks &= ~TrackCrossesTracks(TrackdirToTrack(ft.m_old_td));
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2374
				}
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2375
				ChooseTrainTrack(v, ft.m_new_tile, ft.m_exitdir, tracks, false, NULL, false);
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2376
			}
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2377
		}
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2378
	}
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2379
}
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  2380
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2381
static bool CheckTrainStayInDepot(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2382
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2383
	/* bail out if not all wagons are in the same depot or not in a depot at all */
7492
35acee076719 (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: 7488
diff changeset
  2384
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  2385
		if (u->u.rail.track != TRACK_BIT_DEPOT || u->tile != v->tile) return false;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2386
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2387
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2388
	/* if the train got no power, then keep it in the depot */
4252
89a4206d3cb5 (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
  2389
	if (v->u.rail.cached_power == 0) {
89a4206d3cb5 (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
  2390
		v->vehstatus |= VS_STOPPED;
89a4206d3cb5 (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
  2391
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
89a4206d3cb5 (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
  2392
		return true;
89a4206d3cb5 (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
  2393
	}
89a4206d3cb5 (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
  2394
9818
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2395
	SigSegState seg_state;
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2396
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2397
	if (v->u.rail.force_proceed == 0) {
9818
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2398
		/* force proceed was not pressed */
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2399
		if (++v->load_unload_time_rem < 37) {
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2400
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2401
			return true;
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2402
		}
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2403
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2404
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2405
9818
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2406
		seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2407
		if (seg_state == SIGSEG_FULL || GetDepotWaypointReservation(v->tile)) {
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2408
			/* Full and no PBS signal in block or depot reserved, can't exit. */
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2409
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2410
			return true;
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2411
		}
9818
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2412
	} else {
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2413
		seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2414
	}
2916
b687477adcba (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
  2415
9818
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2416
	/* Only leave when we can reserve a path to our destination. */
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2417
	if (seg_state == SIGSEG_PBS && !TryPathReserve(v) && v->u.rail.force_proceed == 0) {
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2418
		/* No path and no force proceed. */
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2419
		InvalidateWindowClasses(WC_TRAINS_LIST);
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2420
		MarkTrainAsStuck(v);
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2421
		return true;
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2422
	}
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2423
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2424
	SetDepotWaypointReservation(v->tile, true);
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2425
	if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);
bec6961fb6b6 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium
parents: 9817
diff changeset
  2426
578
1e66514eb621 (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
  2427
	VehicleServiceInDepot(v);
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2428
	InvalidateWindowClasses(WC_TRAINS_LIST);
8550
9f90ede990ac (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138
parents: 8510
diff changeset
  2429
	v->PlayLeaveStationSound();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2430
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2431
	v->u.rail.track = TRACK_BIT_X;
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2432
	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
  2433
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2434
	v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2435
	v->cur_speed = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2436
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  2437
	v->UpdateDeltaXY(v->direction);
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7109
diff changeset
  2438
	v->cur_image = v->GetImage(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2439
	VehiclePositionChanged(v);
8300
dfd530665621 (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8283
diff changeset
  2440
	UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2441
	UpdateTrainAcceleration(v);
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  2442
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2443
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2444
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2445
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2446
9809
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2447
/** Clear the reservation of a tile that was just left by a wagon on track_dir. */
10103
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2448
static void ClearPathReservation(const Vehicle *v, TileIndex tile, Trackdir track_dir)
9809
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2449
{
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2450
	DiagDirection dir = TrackdirToExitdir(track_dir);
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2451
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2452
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2453
		/* Are we just leaving a tunnel/bridge? */
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2454
		if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(dir)) {
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2455
			TileIndex end = GetOtherTunnelBridgeEnd(tile);
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2456
10103
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2457
			if (!HasVehicleOnTunnelBridge(tile, end, v)) {
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2458
				/* Free the reservation only if no other train is on the tiles. */
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2459
				SetTunnelBridgeReservation(tile, false);
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2460
				SetTunnelBridgeReservation(end, false);
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2461
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2462
				if (_settings_client.gui.show_track_reservation) {
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2463
					MarkTileDirtyByTile(tile);
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2464
					MarkTileDirtyByTile(end);
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2465
				}
9809
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2466
			}
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2467
		}
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2468
	} else if (IsRailwayStationTile(tile)) {
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2469
		TileIndex new_tile = TileAddByDiagDir(tile, dir);
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2470
		/* If the new tile is not a further tile of the same station, we
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2471
		 * clear the reservation for the whole platform. */
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2472
		if (!IsCompatibleTrainStationTile(new_tile, tile)) {
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2473
			SetRailwayStationPlatformReservation(tile, ReverseDiagDir(dir), false);
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2474
		}
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2475
	} else {
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2476
		/* Any other tile */
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2477
		UnreserveRailTrack(tile, TrackdirToTrack(track_dir));
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2478
	}
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2479
}
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  2480
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2481
/** Free the reserved path in front of a vehicle. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2482
void FreeTrainTrackReservation(const Vehicle *v, TileIndex origin, Trackdir orig_td)
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2483
{
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2484
	assert(IsFrontEngine(v));
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2485
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2486
	TileIndex tile = origin != INVALID_TILE ? origin : v->tile;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2487
	Trackdir  td = orig_td != INVALID_TRACKDIR ? orig_td : GetVehicleTrackdir(v);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2488
	bool      free_tile = tile != v->tile || !(IsRailwayStationTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE));
9974
49ed9790d079 (svn r14131) -Fix (r13952) [FS#2250]: The reservation of adjacent stations was sometimes not freed correctly. (michi_cc)
frosch
parents: 9973
diff changeset
  2489
	StationID station_id = IsRailwayStationTile(v->tile) ? GetStationIndex(v->tile) : INVALID_STATION;
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2490
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2491
	/* Don't free reservation if it's not ours. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2492
	if (TracksOverlap(GetReservedTrackbits(tile) | TrackToTrackBits(TrackdirToTrack(td)))) return;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2493
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2494
	CFollowTrackRail ft(v, GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2495
	while (ft.Follow(tile, td)) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2496
		tile = ft.m_new_tile;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2497
		TrackdirBits bits = (TrackdirBits)(ft.m_new_td_bits & (GetReservedTrackbits(tile) * 0x101));
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2498
		td = RemoveFirstTrackdir(&bits);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2499
		assert(bits == TRACKDIR_BIT_NONE);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2500
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2501
		if (!IsValidTrackdir(td)) break;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2502
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2503
		if (IsTileType(tile, MP_RAILWAY)) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2504
			if (HasSignalOnTrackdir(tile, td) && !IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)))) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2505
				/* Conventional signal along trackdir: remove reservation and stop. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2506
				UnreserveRailTrack(tile, TrackdirToTrack(td));
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2507
				break;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2508
			}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2509
			if (HasPbsSignalOnTrackdir(tile, td)) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2510
				if (GetSignalStateByTrackdir(tile, td) == SIGNAL_STATE_RED) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2511
					/* Red PBS signal? Can't be our reservation, would be green then. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2512
					break;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2513
				} else {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2514
					/* Turn the signal back to red. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2515
					SetSignalStateByTrackdir(tile, td, SIGNAL_STATE_RED);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2516
					MarkTileDirtyByTile(tile);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2517
				}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2518
			} else if (HasSignalOnTrackdir(tile, ReverseTrackdir(td)) && IsOnewaySignal(tile, TrackdirToTrack(td))) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2519
				break;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2520
			}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2521
		}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2522
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2523
		/* Don't free first station/bridge/tunnel if we are on it. */
10103
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  2524
		if (free_tile || (!(ft.m_is_station && GetStationIndex(ft.m_new_tile) == station_id) && !ft.m_is_tunnel && !ft.m_is_bridge)) ClearPathReservation(v, tile, td);
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2525
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2526
		free_tile = true;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2527
	}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2528
}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2529
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2530
/** Check for station tiles */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  2531
struct TrainTrackFollowerData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2532
	TileIndex dest_coords;
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2533
	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
  2534
	uint best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2535
	uint best_track_dist;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2536
	TrackdirByte best_track;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  2537
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2538
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2539
static bool NtpCallbFindStation(TileIndex tile, TrainTrackFollowerData *ttfd, Trackdir track, uint length)
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  2540
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2541
	/* heading for nowhere? */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2928
diff changeset
  2542
	if (ttfd->dest_coords == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2543
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2544
	/* did we reach the final station? */
3315
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  2545
	if ((ttfd->station_index == INVALID_STATION && tile == ttfd->dest_coords) || (
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  2546
				IsTileType(tile, MP_STATION) &&
3334
2999d2ddc0f9 (svn r4113) Moved all relevant map functions for stations to station_map.[ch]
celestar
parents: 3322
diff changeset
  2547
				IsRailwayStation(tile) &&
3315
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  2548
				GetStationIndex(tile) == ttfd->station_index
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
  2549
			)) {
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2550
		/* We do not check for dest_coords if we have a station_index,
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2551
		 * because in that case the dest_coords are just an
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2552
		 * approximation of where the station is */
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2553
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2554
		/* found station */
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2555
		ttfd->best_track = track;
6303
584eb58fe283 (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: 6263
diff changeset
  2556
		ttfd->best_bird_dist = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2557
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2558
	} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2559
		/* didn't find station, keep track of the best path so far. */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2560
		uint dist = DistanceManhattan(tile, ttfd->dest_coords);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2561
		if (dist < ttfd->best_bird_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2562
			ttfd->best_bird_dist = dist;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2563
			ttfd->best_track = track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2564
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2565
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2566
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2567
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2568
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  2569
static void FillWithStationData(TrainTrackFollowerData *fd, const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2570
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2571
	fd->dest_coords = v->dest_tile;
8840
332412c2e9c1 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 8836
diff changeset
  2572
	fd->station_index = v->current_order.IsType(OT_GOTO_STATION) ? v->current_order.GetDestination() : INVALID_STATION;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2573
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2574
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2575
static const byte _initial_tile_subcoord[6][4][3] = {
4344
7e123fec5b0b (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
  2576
{{ 15, 8, 1 }, { 0, 0, 0 }, { 0, 8, 5 }, { 0,  0, 0 }},
7e123fec5b0b (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
  2577
{{  0, 0, 0 }, { 8, 0, 3 }, { 0, 0, 0 }, { 8, 15, 7 }},
7e123fec5b0b (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
  2578
{{  0, 0, 0 }, { 7, 0, 2 }, { 0, 7, 6 }, { 0,  0, 0 }},
7e123fec5b0b (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
  2579
{{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }},
7e123fec5b0b (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
  2580
{{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0,  0, 0 }},
7e123fec5b0b (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
  2581
{{  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
  2582
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2583
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2584
static const byte _search_directions[6][4] = {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2585
	{ 0, 9, 2, 9 }, ///< track 1
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2586
	{ 9, 1, 9, 3 }, ///< track 2
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2587
	{ 9, 0, 3, 9 }, ///< track upper
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2588
	{ 1, 9, 9, 2 }, ///< track lower
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2589
	{ 3, 2, 9, 9 }, ///< track left
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2590
	{ 9, 9, 1, 0 }, ///< track right
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2591
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2592
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2593
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
  2594
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2595
/**
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2596
 * Perform pathfinding for a train.
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2597
 *
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2598
 * @param v The train
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2599
 * @param tile The tile the train is about to enter
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2600
 * @param enterdir Diagonal direction the train is coming from
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2601
 * @param tracks Usable tracks on the new tile
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2602
 * @param path_not_found [out] Set to false if the pathfinder couldn't find a way to the destination
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2603
 * @param do_track_reservation
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2604
 * @param dest [out]
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2605
 * @return The best track the train should follow
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2606
 */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2607
static Track DoTrainPathfind(Vehicle* v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool do_track_reservation, PBSTileInfo *dest)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2608
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2609
	Track best_track;
4870
2d8c7ffbbac9 (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
  2610
2758
e962dd6c3ed4 (svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents: 2752
diff changeset
  2611
#ifdef PF_BENCHMARK
3341
b20541ef8945 (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
  2612
	TIC()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2613
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2614
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2615
	if (path_not_found) *path_not_found = false;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2616
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2617
	uint8 pathfinder = _settings_game.pf.pathfinder_for_trains;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2618
	if (do_track_reservation && pathfinder == VPF_NTP) pathfinder = VPF_NPF;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2619
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2620
	switch (pathfinder) {
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2621
		case VPF_YAPF: { /* YAPF */
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2622
			Trackdir trackdir = YapfChooseRailTrack(v, tile, enterdir, tracks, path_not_found, do_track_reservation, dest);
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2623
			if (trackdir != INVALID_TRACKDIR) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2624
				best_track = TrackdirToTrack(trackdir);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2625
			} else {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2626
				best_track = FindFirstTrack(tracks);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2627
			}
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2628
		} break;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2629
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2630
		case VPF_NPF: { /* NPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2631
			void *perf = NpfBeginInterval();
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2632
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2633
			NPFFindStationOrTileData fstd;
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2634
			NPFFillWithOrderData(&fstd, v, do_track_reservation);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2635
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2636
			PBSTileInfo origin = FollowTrainReservation(v);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2637
			assert(IsValidTrackdir(origin.trackdir));
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2638
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2639
			NPFFoundTargetData ftd = NPFRouteToStationOrTile(origin.tile, origin.trackdir, true, &fstd, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2640
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2641
			if (dest != NULL) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2642
				dest->tile = ftd.node.tile;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2643
				dest->trackdir = (Trackdir)ftd.node.direction;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2644
				dest->okay = ftd.res_okay;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2645
			}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2646
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2647
			if (ftd.best_trackdir == INVALID_TRACKDIR) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2648
				/* We are already at our target. Just do something
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2649
				 * @todo maybe display error?
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2650
				 * @todo: go straight ahead if possible? */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2651
				best_track = FindFirstTrack(tracks);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2652
			} else {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2653
				/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2654
				 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2655
				 * we did not find our target, but ftd.best_trackdir contains the direction leading
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2656
				 * to the tile closest to our target. */
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2657
				if (ftd.best_bird_dist != 0 && path_not_found != NULL) *path_not_found = true;
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2658
				/* Discard enterdir information, making it a normal track */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2659
				best_track = TrackdirToTrack(ftd.best_trackdir);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2660
			}
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2661
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2662
			int time = NpfEndInterval(perf);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2663
			DEBUG(yapf, 4, "[NPFT] %d us - %d rounds - %d open - %d closed -- ", time, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2664
		} break;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2665
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2666
		default:
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2667
		case VPF_NTP: { /* NTP */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2668
			void *perf = NpfBeginInterval();
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2669
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2670
			TrainTrackFollowerData fd;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2671
			FillWithStationData(&fd, v);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2672
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2673
			/* New train pathfinding */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2674
			fd.best_bird_dist = UINT_MAX;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2675
			fd.best_track_dist = UINT_MAX;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2676
			fd.best_track = INVALID_TRACKDIR;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2677
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2678
			NewTrainPathfind(tile - TileOffsByDiagDir(enterdir), v->dest_tile,
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2679
				v->u.rail.compatible_railtypes, enterdir, (NTPEnumProc*)NtpCallbFindStation, &fd);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2680
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2681
			/* check whether the path was found or only 'guessed' */
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2682
			if (fd.best_bird_dist != 0 && path_not_found != NULL) *path_not_found = true;
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2683
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2684
			if (fd.best_track == INVALID_TRACKDIR) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2685
				/* blaha */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2686
				best_track = FindFirstTrack(tracks);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2687
			} else {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2688
				best_track = TrackdirToTrack(fd.best_track);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2689
			}
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2690
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2691
			int time = NpfEndInterval(perf);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2692
			DEBUG(yapf, 4, "[NTPT] %d us - %d rounds - %d open - %d closed -- ", time, 0, 0, 0);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2693
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2694
	}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  2695
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2696
#ifdef PF_BENCHMARK
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2697
	TOC("PF time = ", 1)
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2698
#endif
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2699
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2700
	return best_track;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2701
}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2702
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2703
/**
9817
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2704
 * Extend a train path as far as possible. Stops on encountering a safe tile,
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2705
 * another reservation or a track choice.
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2706
 * @return INVALID_TILE indicates that the reservation failed.
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2707
 */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2708
static PBSTileInfo ExtendTrainReservation(const Vehicle *v, TrackBits *new_tracks, DiagDirection *enterdir)
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2709
{
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2710
	bool no_90deg_turns = _settings_game.pf.pathfinder_for_trains != VPF_NTP && _settings_game.pf.forbid_90_deg;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2711
	PBSTileInfo origin = FollowTrainReservation(v);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2712
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2713
	CFollowTrackRail ft(v);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2714
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2715
	TileIndex tile = origin.tile;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2716
	Trackdir  cur_td = origin.trackdir;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2717
	while (ft.Follow(tile, cur_td)) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2718
		if (KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2719
			/* Possible signal tile. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2720
			if (HasOnewaySignalBlockingTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) break;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2721
		}
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2722
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2723
		if (no_90deg_turns) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2724
			ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(ft.m_old_td);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2725
			if (ft.m_new_td_bits == TRACKDIR_BIT_NONE) break;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2726
		}
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2727
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2728
		/* Station, depot or waypoint are a possible target. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2729
		bool target_seen = ft.m_is_station || (IsTileType(ft.m_new_tile, MP_RAILWAY) && !IsPlainRailTile(ft.m_new_tile));
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2730
		if (target_seen || KillFirstBit(ft.m_new_td_bits) != TRACKDIR_BIT_NONE) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2731
			/* Choice found or possible target encountered.
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2732
			 * On finding a possible target, we need to stop and let the pathfinder handle the
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2733
			 * remaining path. This is because we don't know if this target is in one of our
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2734
			 * orders, so we might cause pathfinding to fail later on if we find a choice.
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2735
			 * This failure would cause a bogous call to TryReserveSafePath which might reserve
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2736
			 * a wrong path not leading to our next destination. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2737
			if (HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(TrackdirReachesTrackdirs(ft.m_old_td)))) break;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2738
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2739
			/* If we did skip some tiles, backtrack to the first skipped tile so the pathfinder
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2740
			 * actually starts its search at the first unreserved tile. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2741
			if (ft.m_tiles_skipped != 0) ft.m_new_tile -= TileOffsByDiagDir(ft.m_exitdir) * ft.m_tiles_skipped;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2742
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2743
			/* Choice found, path valid but not okay. Save info about the choice tile as well. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2744
			if (new_tracks) *new_tracks = TrackdirBitsToTrackBits(ft.m_new_td_bits);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2745
			if (enterdir) *enterdir = ft.m_exitdir;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2746
			return PBSTileInfo(ft.m_new_tile, ft.m_old_td, false);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2747
		}
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2748
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2749
		tile = ft.m_new_tile;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2750
		cur_td = FindFirstTrackdir(ft.m_new_td_bits);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2751
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2752
		if (IsSafeWaitingPosition(v, tile, cur_td, true, no_90deg_turns)) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2753
			bool wp_free = IsWaitingPositionFree(v, tile, cur_td, no_90deg_turns);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2754
			if (!(wp_free && TryReserveRailTrack(tile, TrackdirToTrack(cur_td)))) break;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2755
			/* Safe position is all good, path valid and okay. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2756
			return PBSTileInfo(tile, cur_td, true);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2757
		}
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2758
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2759
		if (!TryReserveRailTrack(tile, TrackdirToTrack(cur_td))) break;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2760
	}
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2761
10186
6e6d49ff4e8c (svn r14397) -Fix: possible buffer overrun, wrong parameter type passed to printf and && where || is meant. Found by MSVC's code analysis (Darkvater)
rubidium
parents: 10178
diff changeset
  2762
	if (ft.m_err == CFollowTrackRail::EC_OWNER || ft.m_err == CFollowTrackRail::EC_NO_WAY) {
9817
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2763
		/* End of line, path valid and okay. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2764
		return PBSTileInfo(ft.m_old_tile, ft.m_old_td, true);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2765
	}
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2766
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2767
	/* Sorry, can't reserve path, back out. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2768
	tile = origin.tile;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2769
	cur_td = origin.trackdir;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2770
	TileIndex stopped = ft.m_old_tile;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2771
	Trackdir  stopped_td = ft.m_old_td;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2772
	while (tile != stopped || cur_td != stopped_td) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2773
		if (!ft.Follow(tile, cur_td)) break;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2774
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2775
		if (no_90deg_turns) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2776
			ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(ft.m_old_td);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2777
			assert(ft.m_new_td_bits != TRACKDIR_BIT_NONE);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2778
		}
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2779
		assert(KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2780
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2781
		tile = ft.m_new_tile;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2782
		cur_td = FindFirstTrackdir(ft.m_new_td_bits);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2783
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2784
		UnreserveRailTrack(tile, TrackdirToTrack(cur_td));
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2785
	}
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2786
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2787
	/* Path invalid. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2788
	return PBSTileInfo();
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2789
}
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2790
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2791
/**
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2792
 * Try to reserve any path to a safe tile, ignoring the vehicle's destination.
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2793
 * Safe tiles are tiles in front of a signal, depots and station tiles at end of line.
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2794
 *
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2795
 * @param v The vehicle.
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2796
 * @param tile The tile the search should start from.
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2797
 * @param td The trackdir the search should start from.
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2798
 * @param override_tailtype Whether all physically compatible railtypes should be followed.
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2799
 * @return True if a path to a safe stopping tile could be reserved.
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2800
 */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2801
static bool TryReserveSafeTrack(const Vehicle* v, TileIndex tile, Trackdir td, bool override_tailtype)
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2802
{
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2803
	if (_settings_game.pf.pathfinder_for_trains == VPF_YAPF) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2804
		return YapfRailFindNearestSafeTile(v, tile, td, override_tailtype);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2805
	} else {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2806
		return NPFRouteToSafeTile(v, tile, td, override_tailtype).res_okay;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2807
	}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2808
}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2809
10251
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2810
/** This class will save the current order of a vehicle and restore it on destruction. */
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2811
class VehicleOrderSaver
9812
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  2812
{
10251
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2813
private:
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2814
	Vehicle        *v;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2815
	Order          old_order;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2816
	TileIndex      old_dest_tile;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2817
	VehicleOrderID index;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2818
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2819
public:
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2820
	VehicleOrderSaver(Vehicle *_v) : v(_v), old_order(_v->current_order), old_dest_tile(_v->dest_tile), index(_v->cur_order_index)
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2821
	{
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2822
	}
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2823
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2824
	~VehicleOrderSaver()
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2825
	{
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2826
		this->v->current_order = this->old_order;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2827
		this->v->dest_tile = this->old_dest_tile;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2828
	}
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2829
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2830
	/**
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2831
	 * Set the current vehicle order to the next order in the order list.
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2832
	 * @return True if a suitable next order could be found.
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2833
	 */
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2834
	bool SwitchToNextOrder()
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2835
	{
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2836
		++this->index;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2837
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2838
		do {
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2839
			/* Wrap around. */
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2840
			if (this->index >= this->v->num_orders) this->index = 0;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2841
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2842
			Order *order = GetVehicleOrder(this->v, this->index);
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2843
			assert(order != NULL);
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2844
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2845
			switch (order->GetType()) {
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2846
				case OT_GOTO_DEPOT:
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2847
					/* Skip service in depot orders when the train doesn't need service. */
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2848
					if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !this->v->NeedsServicing()) break;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2849
				case OT_GOTO_STATION:
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2850
				case OT_GOTO_WAYPOINT:
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2851
					this->v->current_order = *order;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2852
					UpdateOrderDest(this->v, order);
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2853
					return true;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2854
				case OT_CONDITIONAL: {
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2855
					VehicleOrderID next = ProcessConditionalOrder(order, this->v);
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2856
					if (next != INVALID_VEH_ORDER_ID) {
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2857
						this->index = next;
10261
b9f3d98e717d (svn r14492) -Fix [FS#2366] (r14482): Conditional orders could lead to an infinite loop on path look-ahead.
michi_cc
parents: 10252
diff changeset
  2858
						/* Don't increment next, so no break here. */
10251
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2859
						continue;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2860
					}
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2861
					break;
9812
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  2862
				}
10251
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2863
				default:
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2864
					break;
9812
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  2865
			}
10261
b9f3d98e717d (svn r14492) -Fix [FS#2366] (r14482): Conditional orders could lead to an infinite loop on path look-ahead.
michi_cc
parents: 10252
diff changeset
  2866
			/* Don't increment inside the while because otherwise conditional
b9f3d98e717d (svn r14492) -Fix [FS#2366] (r14482): Conditional orders could lead to an infinite loop on path look-ahead.
michi_cc
parents: 10252
diff changeset
  2867
			 * orders can lead to an infinite loop. */
b9f3d98e717d (svn r14492) -Fix [FS#2366] (r14482): Conditional orders could lead to an infinite loop on path look-ahead.
michi_cc
parents: 10252
diff changeset
  2868
			++this->index;
b9f3d98e717d (svn r14492) -Fix [FS#2366] (r14482): Conditional orders could lead to an infinite loop on path look-ahead.
michi_cc
parents: 10252
diff changeset
  2869
		} while (this->index != this->v->cur_order_index);
10251
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2870
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2871
		return false;
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2872
	}
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  2873
};
9812
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  2874
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2875
/* choose a track */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2876
static Track ChooseTrainTrack(Vehicle* v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck)
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2877
{
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2878
	Track best_track = INVALID_TRACK;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2879
	bool do_track_reservation = _settings_game.pf.reserve_paths || force_res;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2880
	bool changed_signal = false;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2881
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2882
	assert((tracks & ~TRACK_BIT_MASK) == 0);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2883
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2884
	if (got_reservation != NULL) *got_reservation = false;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2885
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2886
	/* Don't use tracks here as the setting to forbid 90 deg turns might have been switched between reservation and now. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2887
	TrackBits res_tracks = (TrackBits)(GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir));
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2888
	/* Do we have a suitable reserved track? */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2889
	if (res_tracks != TRACK_BIT_NONE) return FindFirstTrack(res_tracks);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2890
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2891
	/* Quick return in case only one possible track is available */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2892
	if (KillFirstBit(tracks) == TRACK_BIT_NONE) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2893
		Track track = FindFirstTrack(tracks);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2894
		/* We need to check for signals only here, as a junction tile can't have signals. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2895
		if (track != INVALID_TRACK && HasPbsSignalOnTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir))) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2896
			do_track_reservation = true;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2897
			changed_signal = true;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2898
			SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir), SIGNAL_STATE_GREEN);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2899
		} else if (!do_track_reservation) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2900
			return track;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2901
		}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2902
		best_track = track;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2903
	}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2904
9817
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2905
	PBSTileInfo   res_dest(tile, INVALID_TRACKDIR, false);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2906
	DiagDirection dest_enterdir = enterdir;
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2907
	if (do_track_reservation) {
9822
ab2bd3cf424f (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium
parents: 9821
diff changeset
  2908
		/* Check if the train needs service here, so it has a chance to always find a depot.
ab2bd3cf424f (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium
parents: 9821
diff changeset
  2909
		 * Also check if the current order is a service order so we don't reserve a path to
ab2bd3cf424f (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium
parents: 9821
diff changeset
  2910
		 * the destination but instead to the next one if service isn't needed. */
ab2bd3cf424f (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium
parents: 9821
diff changeset
  2911
		CheckIfTrainNeedsService(v);
ab2bd3cf424f (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium
parents: 9821
diff changeset
  2912
		if (v->current_order.IsType(OT_DUMMY) || v->current_order.IsType(OT_CONDITIONAL) || v->current_order.IsType(OT_GOTO_DEPOT)) ProcessOrders(v);
9817
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2913
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2914
		res_dest = ExtendTrainReservation(v, &tracks, &dest_enterdir);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2915
		if (res_dest.tile == INVALID_TILE) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2916
			/* Reservation failed? */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2917
			if (mark_stuck) MarkTrainAsStuck(v);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2918
			if (changed_signal) SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(best_track, enterdir), SIGNAL_STATE_RED);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2919
			return FindFirstTrack(tracks);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2920
		}
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2921
	}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2922
10252
3ae02dea2fda (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc
parents: 10251
diff changeset
  2923
	/* Save the current train order. The destructor will restore the old order on function exit. */
3ae02dea2fda (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc
parents: 10251
diff changeset
  2924
	VehicleOrderSaver orders(v);
3ae02dea2fda (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc
parents: 10251
diff changeset
  2925
3ae02dea2fda (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc
parents: 10251
diff changeset
  2926
	/* If the current tile is the destination of the current order and
10307
86ce4ec269d4 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc
parents: 10287
diff changeset
  2927
	 * a reservation was requested, advance to the next order.
86ce4ec269d4 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc
parents: 10287
diff changeset
  2928
	 * Don't advance on a depot order as depots are always safe end points
86ce4ec269d4 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc
parents: 10287
diff changeset
  2929
	 * for a path and no look-ahead is necessary. This also avoids a
86ce4ec269d4 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc
parents: 10287
diff changeset
  2930
	 * problem with depot orders not part of the order list when the
86ce4ec269d4 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc
parents: 10287
diff changeset
  2931
	 * order list itself is empty. */
86ce4ec269d4 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc
parents: 10287
diff changeset
  2932
	if (!v->current_order.IsType(OT_GOTO_DEPOT) && (v->tile == v->dest_tile || (v->current_order.IsType(OT_GOTO_STATION) && IsRailwayStationTile(v->tile) && v->current_order.GetDestination() == GetStationIndex(v->tile)))) {
10252
3ae02dea2fda (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc
parents: 10251
diff changeset
  2933
		orders.SwitchToNextOrder();
3ae02dea2fda (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc
parents: 10251
diff changeset
  2934
	}
3ae02dea2fda (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc
parents: 10251
diff changeset
  2935
9817
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2936
	if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2937
		/* Pathfinders are able to tell that route was only 'guessed'. */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2938
		bool      path_not_found = false;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2939
		TileIndex new_tile = res_dest.tile;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2940
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2941
		Track next_track = DoTrainPathfind(v, new_tile, dest_enterdir, tracks, &path_not_found, do_track_reservation, &res_dest);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2942
		if (new_tile == tile) best_track = next_track;
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2943
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2944
		/* handle "path not found" state */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2945
		if (path_not_found) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2946
			/* PF didn't find the route */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2947
			if (!HasBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2948
				/* it is first time the problem occurred, set the "path not found" flag */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2949
				SetBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2950
				/* and notify user about the event */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
  2951
				if (_settings_client.gui.lost_train_warn && v->owner == _local_company) {
9817
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2952
					SetDParam(0, v->unitnumber);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2953
					AddNewsItem(
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2954
						STR_TRAIN_IS_LOST,
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2955
						NS_ADVICE,
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2956
						v->index,
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2957
						0);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2958
				}
4870
2d8c7ffbbac9 (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
  2959
			}
9817
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2960
		} else {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2961
			/* route found, is the train marked with "path not found" flag? */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2962
			if (HasBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) {
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2963
				/* clear the flag as the PF's problem was solved */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2964
				ClrBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION);
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2965
				/* can we also delete the "News" item somehow? */
b1da06062664 (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium
parents: 9816
diff changeset
  2966
			}
4870
2d8c7ffbbac9 (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
  2967
		}
2d8c7ffbbac9 (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
  2968
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2969
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2970
	/* No track reservation requested -> finished. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2971
	if (!do_track_reservation) return best_track;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2972
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2973
	/* A path was found, but could not be reserved. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2974
	if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2975
		if (mark_stuck) MarkTrainAsStuck(v);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2976
		FreeTrainTrackReservation(v);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2977
		return best_track;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2978
	}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2979
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2980
	/* No possible reservation target found, we are probably lost. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2981
	if (res_dest.tile == INVALID_TILE) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2982
		/* Try to find any safe destination. */
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2983
		PBSTileInfo origin = FollowTrainReservation(v);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2984
		if (TryReserveSafeTrack(v, origin.tile, origin.trackdir, false)) {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2985
			TrackBits res = GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2986
			best_track = FindFirstTrack(res);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2987
			TryReserveRailTrack(v->tile, TrackdirToTrack(GetVehicleTrackdir(v)));
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2988
			if (got_reservation != NULL) *got_reservation = true;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2989
			if (changed_signal) MarkTileDirtyByTile(tile);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2990
		} else {
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2991
			FreeTrainTrackReservation(v);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2992
			if (mark_stuck) MarkTrainAsStuck(v);
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2993
		}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2994
		return best_track;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2995
	}
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2996
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2997
	if (got_reservation != NULL) *got_reservation = true;
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  2998
9812
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  2999
	/* Reservation target found and free, check if it is safe. */
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3000
	while (!IsSafeWaitingPosition(v, res_dest.tile, res_dest.trackdir, true, _settings_game.pf.forbid_90_deg)) {
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3001
		/* Extend reservation until we have found a safe position. */
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3002
		DiagDirection exitdir = TrackdirToExitdir(res_dest.trackdir);
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3003
		TileIndex     next_tile = TileAddByDiagDir(res_dest.tile, exitdir);
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3004
		TrackBits     reachable = TrackdirBitsToTrackBits((TrackdirBits)(GetTileTrackStatus(next_tile, TRANSPORT_RAIL, 0))) & DiagdirReachesTracks(exitdir);
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3005
		if (_settings_game.pf.pathfinder_for_trains != VPF_NTP && _settings_game.pf.forbid_90_deg) {
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3006
			reachable &= ~TrackCrossesTracks(TrackdirToTrack(res_dest.trackdir));
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3007
		}
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3008
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3009
		/* Get next order with destination. */
10251
1f6cc1c5c8b0 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc
parents: 10236
diff changeset
  3010
		if (orders.SwitchToNextOrder()) {
9812
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3011
			PBSTileInfo cur_dest;
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3012
			DoTrainPathfind(v, next_tile, exitdir, reachable, NULL, true, &cur_dest);
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3013
			if (cur_dest.tile != INVALID_TILE) {
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3014
				res_dest = cur_dest;
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3015
				if (res_dest.okay) continue;
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3016
				/* Path found, but could not be reserved. */
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3017
				FreeTrainTrackReservation(v);
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3018
				if (mark_stuck) MarkTrainAsStuck(v);
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3019
				if (got_reservation != NULL) *got_reservation = false;
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3020
				changed_signal = false;
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3021
				break;
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3022
			}
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3023
		}
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3024
		/* No order or no safe position found, try any position. */
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3025
		if (!TryReserveSafeTrack(v, res_dest.tile, res_dest.trackdir, true)) {
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3026
			FreeTrainTrackReservation(v);
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3027
			if (mark_stuck) MarkTrainAsStuck(v);
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3028
			if (got_reservation != NULL) *got_reservation = false;
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3029
			changed_signal = false;
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3030
		}
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3031
		break;
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3032
	}
8ad2baf19438 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium
parents: 9811
diff changeset
  3033
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  3034
	TryReserveRailTrack(v->tile, TrackdirToTrack(GetVehicleTrackdir(v)));
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  3035
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  3036
	if (changed_signal) MarkTileDirtyByTile(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3037
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3038
	return best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3039
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3040
9813
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3041
/**
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3042
 * Try to reserve a path to a safe position.
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3043
 *
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3044
 * @param v The vehicle
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3045
 * @return True if a path could be reserved
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3046
 */
9828
cbb4ae9abfe8 (svn r13971) -Fix [YAPP]: segfault due to mixed up variables (michi_cc)
rubidium
parents: 9824
diff changeset
  3047
bool TryPathReserve(Vehicle *v, bool mark_as_stuck, bool first_tile_okay)
9813
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3048
{
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3049
	assert(v->type == VEH_TRAIN && IsFrontEngine(v));
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3050
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3051
	/* We have to handle depots specially as the track follower won't look
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3052
	 * at the depot tile itself but starts from the next tile. If we are still
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3053
	 * inside the depot, a depot reservation can never be ours. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3054
	if (v->u.rail.track & TRACK_BIT_DEPOT) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3055
		if (GetDepotWaypointReservation(v->tile)) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3056
			if (mark_as_stuck) MarkTrainAsStuck(v);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3057
			return false;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3058
		} else {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3059
			/* Depot not reserved, but the next tile might be. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3060
			TileIndex next_tile = TileAddByDiagDir(v->tile, GetRailDepotDirection(v->tile));
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3061
			if (HasReservedTracks(next_tile, DiagdirReachesTracks(GetRailDepotDirection(v->tile)))) return false;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3062
		}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3063
	}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3064
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3065
	/* Special check if we are in front of a two-sided conventional signal. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3066
	DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3067
	TileIndex next_tile = TileAddByDiagDir(v->tile, dir);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3068
	if (IsTileType(next_tile, MP_RAILWAY) && HasReservedTracks(next_tile, DiagdirReachesTracks(dir))) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3069
		/* Can have only one reserved trackdir. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3070
		Trackdir td = FindFirstTrackdir((TrackdirBits)(GetReservedTrackbits(next_tile) * 0x101 & DiagdirReachesTrackdirs(dir)));
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3071
		if (HasSignalOnTrackdir(next_tile, td) && HasSignalOnTrackdir(next_tile, ReverseTrackdir(td)) &&
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3072
				!IsPbsSignal(GetSignalType(next_tile, TrackdirToTrack(td)))) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3073
			/* Signal already reserved, is not ours. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3074
			if (mark_as_stuck) MarkTrainAsStuck(v);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3075
			return false;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3076
		}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3077
	}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3078
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  3079
	bool other_train = false;
9831
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9828
diff changeset
  3080
	PBSTileInfo origin = FollowTrainReservation(v, &other_train);
9813
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3081
	/* If we have a reserved path and the path ends at a safe tile, we are finished already. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3082
	if (origin.okay && (v->tile != origin.tile || first_tile_okay)) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3083
		/* Can't be stuck then. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3084
		if (HasBit(v->u.rail.flags, VRF_TRAIN_STUCK)) InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3085
		ClrBit(v->u.rail.flags, VRF_TRAIN_STUCK);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3086
		return true;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3087
	}
9831
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9828
diff changeset
  3088
	/* The path we are driving on is alread blocked by some other train.
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9828
diff changeset
  3089
	 * This can only happen when tracks and signals are changed. A crash
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9828
diff changeset
  3090
	 * is probably imminent, don't do any further reservation because
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9828
diff changeset
  3091
	 * it might cause stale reservations. */
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  3092
	if (other_train && v->tile != origin.tile) {
9831
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9828
diff changeset
  3093
		if (mark_as_stuck) MarkTrainAsStuck(v);
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9828
diff changeset
  3094
		return false;
b233da1b1723 (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch
parents: 9828
diff changeset
  3095
	}
9813
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3096
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3097
	/* If we are in a depot, tentativly reserve the depot. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3098
	if (v->u.rail.track & TRACK_BIT_DEPOT) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3099
		SetDepotWaypointReservation(v->tile, true);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3100
		if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3101
	}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3102
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3103
	DiagDirection exitdir = TrackdirToExitdir(origin.trackdir);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3104
	TileIndex     new_tile = TileAddByDiagDir(origin.tile, exitdir);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3105
	TrackBits     reachable = TrackdirBitsToTrackBits((TrackdirBits)GetTileTrackStatus(new_tile, TRANSPORT_RAIL, 0) & DiagdirReachesTrackdirs(exitdir));
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3106
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3107
	if (_settings_game.pf.pathfinder_for_trains != VPF_NTP && _settings_game.pf.forbid_90_deg) reachable &= ~TrackCrossesTracks(TrackdirToTrack(origin.trackdir));
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3108
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3109
	bool res_made = false;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3110
	ChooseTrainTrack(v, new_tile, exitdir, reachable, true, &res_made, mark_as_stuck);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3111
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3112
	if (!res_made) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3113
		/* Free the depot reservation as well. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3114
		if (v->u.rail.track & TRACK_BIT_DEPOT) SetDepotWaypointReservation(v->tile, false);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3115
		return false;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3116
	}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3117
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3118
	if (HasBit(v->u.rail.flags, VRF_TRAIN_STUCK)) InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3119
	ClrBit(v->u.rail.flags, VRF_TRAIN_STUCK);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3120
	return true;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3121
}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  3122
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3123
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3124
static bool CheckReverseTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3125
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  3126
	if (_settings_game.difficulty.line_reverse_mode != 0 ||
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3127
			v->u.rail.track == TRACK_BIT_DEPOT || v->u.rail.track == TRACK_BIT_WORMHOLE ||
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3128
			!(v->direction & 1)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3129
		return false;
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3130
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3131
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3132
	TrainTrackFollowerData fd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3133
	FillWithStationData(&fd, v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3134
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3135
	uint reverse_best = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3136
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3137
	assert(v->u.rail.track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3138
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  3139
	switch (_settings_game.pf.pathfinder_for_trains) {
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3140
		case VPF_YAPF: /* YAPF */
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3141
			reverse_best = YapfCheckReverseTrain(v);
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3142
			break;
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3143
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3144
		case VPF_NPF: { /* NPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3145
			NPFFindStationOrTileData fstd;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3146
			NPFFoundTargetData ftd;
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3147
			Vehicle *last = GetLastVehicleInChain(v);
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3148
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3149
			NPFFillWithOrderData(&fstd, v);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3150
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3151
			Trackdir trackdir = GetVehicleTrackdir(v);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3152
			Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3153
			assert(trackdir != INVALID_TRACKDIR);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3154
			assert(trackdir_rev != INVALID_TRACKDIR);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3155
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3156
			ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3157
			if (ftd.best_bird_dist != 0) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3158
				/* We didn't find anything, just keep on going straight ahead */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3159
				reverse_best = false;
3017
a75caf4efa2d (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
  3160
			} else {
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3161
				if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3162
					reverse_best = true;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3163
				} else {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3164
					reverse_best = false;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3165
				}
3017
a75caf4efa2d (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
  3166
			}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3167
		} break;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3168
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3169
		default:
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3170
		case VPF_NTP: { /* NTP */
8794
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  3171
			int i = _search_directions[FindFirstTrack(v->u.rail.track)][DirToDiagDir(v->direction)];
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  3172
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3173
			int best_track = -1;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3174
			uint reverse = 0;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3175
			uint best_bird_dist  = 0;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3176
			uint best_track_dist = 0;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3177
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3178
			for (;;) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3179
				fd.best_bird_dist = UINT_MAX;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3180
				fd.best_track_dist = UINT_MAX;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3181
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3182
				NewTrainPathfind(v->tile, v->dest_tile, v->u.rail.compatible_railtypes, (DiagDirection)(reverse ^ i), (NTPEnumProc*)NtpCallbFindStation, &fd);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3183
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3184
				if (best_track != -1) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3185
					if (best_bird_dist != 0) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3186
						if (fd.best_bird_dist != 0) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3187
							/* neither reached the destination, pick the one with the smallest bird dist */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3188
							if (fd.best_bird_dist > best_bird_dist) goto bad;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3189
							if (fd.best_bird_dist < best_bird_dist) goto good;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3190
						} else {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3191
							/* we found the destination for the first time */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3192
							goto good;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3193
						}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3194
					} else {
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3195
						if (fd.best_bird_dist != 0) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3196
							/* didn't find destination, but we've found the destination previously */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3197
							goto bad;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3198
						} else {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3199
							/* both old & new reached the destination, compare track length */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3200
							if (fd.best_track_dist > best_track_dist) goto bad;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3201
							if (fd.best_track_dist < best_track_dist) goto good;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3202
						}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3203
					}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3204
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3205
					/* if we reach this position, there's two paths of equal value so far.
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3206
					 * pick one randomly. */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3207
					int r = GB(Random(), 0, 8);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3208
					if (_pick_track_table[i] == (v->direction & 3)) r += 80;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3209
					if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3210
					if (r <= 127) goto bad;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3211
				}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3212
good:;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3213
				best_track = i;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3214
				best_bird_dist = fd.best_bird_dist;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3215
				best_track_dist = fd.best_track_dist;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3216
				reverse_best = reverse;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3217
bad:;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3218
				if (reverse != 0) break;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3219
				reverse = 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3220
			}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8553
diff changeset
  3221
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3222
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3223
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3224
	return reverse_best != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3225
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3226
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8794
diff changeset
  3227
TileIndex Train::GetOrderStationLocation(StationID station)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3228
{
8830
b288359ab100 (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 8827
diff changeset
  3229
	if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
b288359ab100 (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 8827
diff changeset
  3230
8928
78d869931d0d (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium
parents: 8891
diff changeset
  3231
	const Station *st = GetStation(station);
78d869931d0d (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium
parents: 8891
diff changeset
  3232
	if (!(st->facilities & FACIL_TRAIN)) {
78d869931d0d (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium
parents: 8891
diff changeset
  3233
		/* The destination station has no trainstation tiles. */
78d869931d0d (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium
parents: 8891
diff changeset
  3234
		this->cur_order_index++;
78d869931d0d (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium
parents: 8891
diff changeset
  3235
		return 0;
78d869931d0d (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium
parents: 8891
diff changeset
  3236
	}
78d869931d0d (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium
parents: 8891
diff changeset
  3237
78d869931d0d (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium
parents: 8891
diff changeset
  3238
	return st->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3239
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3240
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  3241
void Train::MarkDirty()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3242
{
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  3243
	Vehicle *v = this;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3244
	do {
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7109
diff changeset
  3245
		v->cur_image = v->GetImage(v->direction);
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8312
diff changeset
  3246
		MarkSingleVehicleDirty(v);
7492
35acee076719 (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: 7488
diff changeset
  3247
	} while ((v = v->Next()) != NULL);
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  3248
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  3249
	/* need to update acceleration and cached values since the goods on the train changed. */
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  3250
	TrainCargoChanged(this);
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  3251
	UpdateTrainAcceleration(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3252
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3253
10214
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3254
/**
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3255
 * This function looks at the vehicle and updates it's speed (cur_speed
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3256
 * and subspeed) variables. Furthermore, it returns the distance that
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3257
 * the train can drive this tick. This distance is expressed as 256 * n,
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3258
 * where n is the number of straight (long) tracks the train can
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3259
 * traverse. This means that moving along a straight track costs 256
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3260
 * "speed" and a diagonal track costs 192 "speed".
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3261
 * @param v The vehicle to update the speed of.
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3262
 * @return distance to drive.
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3263
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3264
static int UpdateTrainSpeed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3265
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3266
	uint accel;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3267
9808
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  3268
	if (v->vehstatus & VS_STOPPED || HasBit(v->u.rail.flags, VRF_REVERSING) || HasBit(v->u.rail.flags, VRF_TRAIN_STUCK)) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  3269
		if (_settings_game.vehicle.realistic_acceleration) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  3270
			accel = GetTrainAcceleration(v, AM_BRAKE) * 2;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3271
		} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  3272
			accel = v->acceleration * -2;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3273
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3274
	} else {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  3275
		if (_settings_game.vehicle.realistic_acceleration) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  3276
			accel = GetTrainAcceleration(v, AM_ACCEL);
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3277
		} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  3278
			accel = v->acceleration;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3279
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3280
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3281
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3282
	uint spd = v->subspeed + accel * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3283
	v->subspeed = (byte)spd;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  3284
	{
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  3285
		int tempmax = v->max_speed;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  3286
		if (v->cur_speed > v->max_speed)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  3287
			tempmax = v->cur_speed - (v->cur_speed / 10) - 1;
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7881
diff changeset
  3288
		v->cur_speed = spd = Clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  3289
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3290
10214
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3291
	/* Scale speed by 3/4. Previously this was only done when the train was
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3292
	 * facing diagonally and would apply to however many moves the train made
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3293
	 * regardless the of direction actually moved in. Now it is always scaled,
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3294
	 * 256 spd is used to go straight and 192 is used to go diagonally
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3295
	 * (3/4 of 256). This results in the same effect, but without the error the
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3296
	 * previous method caused.
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3297
	 *
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3298
	 * The scaling is done in this direction and not by multiplying the amount
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3299
	 * to be subtracted by 4/3 so that the leftover speed can be saved in a
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3300
	 * byte in v->progress.
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3301
	 */
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3302
	int scaled_spd = spd * 3 >> 2;
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3303
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3304
	scaled_spd += v->progress;
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3305
	v->progress = 0; // set later in TrainLocoHandler or TrainController
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  3306
	return scaled_spd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3307
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3308
1551
b1c7df6daa63 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  3309
static void TrainEnterStation(Vehicle *v, StationID station)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3310
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3311
	v->last_station_visited = station;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3312
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3313
	/* check if a train ever visited this station before */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3314
	Station *st = GetStation(station);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3315
	if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3316
		st->had_vehicle_of_type |= HVOT_TRAIN;
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  3317
		SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3318
		AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3319
			STR_8801_CITIZENS_CELEBRATE_FIRST,
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
  3320
			v->owner == _local_company ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3321
			v->index,
10123
f73d96619303 (svn r14307) -Fix: when deleting a station, remove news items regarding it
smatz
parents: 10103
diff changeset
  3322
			st->index
3017
a75caf4efa2d (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
  3323
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3324
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3325
6550
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  3326
	v->BeginLoading();
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 9000
diff changeset
  3327
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 9000
diff changeset
  3328
	StationAnimationTrigger(st, v->tile, STAT_ANIM_TRAIN_ARRIVES);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3329
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3330
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  3331
static byte AfterSetTrainPos(Vehicle *v, bool new_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3332
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3333
	byte old_z = v->z_pos;
7079
9cdb1b4f9928 (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: 7070
diff changeset
  3334
	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
  3335
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  3336
	if (new_tile) {
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  3337
		ClrBit(v->u.rail.flags, VRF_GOINGUP);
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  3338
		ClrBit(v->u.rail.flags, VRF_GOINGDOWN);
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  3339
7079
9cdb1b4f9928 (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: 7070
diff changeset
  3340
		if (v->u.rail.track == TRACK_BIT_X || v->u.rail.track == TRACK_BIT_Y) {
9cdb1b4f9928 (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: 7070
diff changeset
  3341
			/* Any track that isn't TRACK_BIT_X or TRACK_BIT_Y cannot be sloped.
7052
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3342
			 * To check whether the current tile is sloped, and in which
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3343
			 * direction it is sloped, we get the 'z' at the center of
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3344
			 * the tile (middle_z) and the edge of the tile (old_z),
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3345
			 * which we then can compare. */
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3346
			static const int HALF_TILE_SIZE = TILE_SIZE / 2;
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3347
			static const int INV_TILE_SIZE_MASK = ~(TILE_SIZE - 1);
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3348
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3349
			byte middle_z = GetSlopeZ((v->x_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE, (v->y_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE);
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3350
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3351
			/* For some reason tunnel tiles are always given as sloped :(
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  3352
			 * But they are not sloped... */
7079
9cdb1b4f9928 (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: 7070
diff changeset
  3353
			if (middle_z != v->z_pos && !IsTunnelTile(TileVirtXY(v->x_pos, v->y_pos))) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  3354
				SetBit(v->u.rail.flags, (middle_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN);
3184
118a520164e4 (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
  3355
			}
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
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
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3360
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3361
	return old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3362
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3363
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  3364
static const Direction _new_vehicle_direction_table[11] = {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  3365
	DIR_N , DIR_NW, DIR_W , INVALID_DIR,
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  3366
	DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  3367
	DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3368
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3369
8607
71d80729f3cf (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 8606
diff changeset
  3370
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
  3371
{
926
a6d140a6a4de (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
  3372
	uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
a6d140a6a4de (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
  3373
							TileX(new_tile) - TileX(old_tile) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3374
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3375
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3376
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3377
8607
71d80729f3cf (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 8606
diff changeset
  3378
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
  3379
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3380
	byte offs;
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
	x -= v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3383
	if (x >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3384
		offs = (x > 2) ? 0 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3385
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3386
		offs = (x < -2) ? 2 : 1;
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
	y -= v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3390
	if (y >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3391
		offs += ((y > 2) ? 0 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3392
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3393
		offs += ((y < -2) ? 2 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3394
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3395
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3396
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3397
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3398
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3399
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3400
/* Check if the vehicle is compatible with the specified tile */
8607
71d80729f3cf (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 8606
diff changeset
  3401
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
  3402
{
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  3403
	return
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  3404
		IsTileOwner(tile, v->owner) && (
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3405
			!IsFrontEngine(v) ||
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  3406
			HasBit(v->u.rail.compatible_railtypes, GetRailType(tile))
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  3407
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3408
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3409
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  3410
struct RailtypeSlowdownParams {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3411
	byte small_turn, large_turn;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3412
	byte z_up; // fraction to remove when moving up
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3413
	byte z_down; // fraction to remove when moving down
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  3414
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3415
3355
e414a0b104a6 (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
  3416
static const RailtypeSlowdownParams _railtype_slowdown[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3417
	// normal accel
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3418
	{256 / 4, 256 / 2, 256 / 4, 2}, ///< normal
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3419
	{256 / 4, 256 / 2, 256 / 4, 2}, ///< electrified
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3420
	{256 / 4, 256 / 2, 256 / 4, 2}, ///< monorail
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3421
	{0,       256 / 2, 256 / 4, 2}, ///< maglev
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3422
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3423
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3424
/** Modify the speed of the vehicle due to a turn */
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3425
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
  3426
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  3427
	if (_settings_game.vehicle.realistic_acceleration) return;
3158
696a6ca0bfa9 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  3428
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3429
	DirDiff diff = DirDifference(v->direction, new_dir);
3158
696a6ca0bfa9 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  3430
	if (diff == DIRDIFF_SAME) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3431
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3432
	const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->u.rail.railtype];
3158
696a6ca0bfa9 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  3433
	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
  3434
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3435
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3436
/** Modify the speed of the vehicle due to a change in altitude */
8607
71d80729f3cf (svn r12189) -Codechange: mark some functions in train_cmd.cpp inline (called once or very short)
smatz
parents: 8606
diff changeset
  3437
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
  3438
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  3439
	if (old_z == v->z_pos || _settings_game.vehicle.realistic_acceleration) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3440
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3441
	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
  3442
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3443
	if (old_z < v->z_pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3444
		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
  3445
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3446
		uint16 spd = v->cur_speed + rsp->z_down;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3447
		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
  3448
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3449
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3450
9821
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3451
static bool TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3452
{
3267
feff95208a9f (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3259
diff changeset
  3453
	if (IsTileType(tile, MP_RAILWAY) &&
3792
67c865c9315c (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
  3454
			GetRailTileType(tile) == RAIL_TILE_SIGNALS) {
8794
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  3455
		TrackdirBits tracks = TrackBitsToTrackdirBits(GetTrackBits(tile)) & DiagdirReachesTrackdirs(dir);
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  3456
		Trackdir trackdir = FindFirstTrackdir(tracks);
9821
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3457
		if (UpdateSignalsOnSegment(tile,  TrackdirToExitdir(trackdir), GetTileOwner(tile)) == SIGSEG_PBS && HasSignalOnTrackdir(tile, trackdir)) {
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3458
			/* A PBS block with a non-PBS signal facing us? */
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3459
			if (!IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) return true;
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3460
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3461
	}
9821
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3462
	return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3463
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3464
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3465
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3466
static void SetVehicleCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3467
{
3017
a75caf4efa2d (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
  3468
	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
  3469
9811
1942cbc26a51 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium
parents: 9810
diff changeset
  3470
	/* Free a possible path reservation and try to mark all tiles occupied by the train reserved. */
1942cbc26a51 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium
parents: 9810
diff changeset
  3471
	if (IsFrontEngine(v)) {
1942cbc26a51 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium
parents: 9810
diff changeset
  3472
		/* Remove all reservations, also the ones currently under the train
1942cbc26a51 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium
parents: 9810
diff changeset
  3473
		 * and any railway station paltform reservation. */
1942cbc26a51 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium
parents: 9810
diff changeset
  3474
		FreeTrainTrackReservation(v);
1942cbc26a51 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium
parents: 9810
diff changeset
  3475
		for (const Vehicle *u = v; u != NULL; u = u->Next()) {
10103
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  3476
			ClearPathReservation(u, u->tile, GetVehicleTrackdir(u));
9845
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3477
			if (IsTileType(u->tile, MP_TUNNELBRIDGE)) {
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3478
				/* ClearPathReservation will not free the wormhole exit
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3479
				 * if the train has just entered the wormhole. */
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3480
				SetTunnelBridgeReservation(GetOtherTunnelBridgeEnd(u->tile), false);
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3481
			}
9811
1942cbc26a51 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium
parents: 9810
diff changeset
  3482
		}
1942cbc26a51 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium
parents: 9810
diff changeset
  3483
	}
1942cbc26a51 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium
parents: 9810
diff changeset
  3484
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3485
	/* we may need to update crossing we were approaching */
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3486
	TileIndex crossing = TrainApproachingCrossingTile(v);
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3487
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3488
	v->u.rail.crash_anim_pos++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3489
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8344
diff changeset
  3490
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
8271
1763441dbe82 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8270
diff changeset
  3491
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
1763441dbe82 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8270
diff changeset
  3492
1763441dbe82 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8270
diff changeset
  3493
	if (v->u.rail.track == TRACK_BIT_DEPOT) {
1763441dbe82 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8270
diff changeset
  3494
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
1763441dbe82 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8270
diff changeset
  3495
	}
1763441dbe82 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8270
diff changeset
  3496
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9274
diff changeset
  3497
	InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
8271
1763441dbe82 (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz
parents: 8270
diff changeset
  3498
9321
61fd5b6e27ce (svn r13205) -Codechange: Remove unnecessary code-style-buggering-up macro.
peter1138
parents: 9297
diff changeset
  3499
	for (; v != NULL; v = v->Next()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3500
		v->vehstatus |= VS_CRASHED;
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8312
diff changeset
  3501
		MarkSingleVehicleDirty(v);
9321
61fd5b6e27ce (svn r13205) -Codechange: Remove unnecessary code-style-buggering-up macro.
peter1138
parents: 9297
diff changeset
  3502
	}
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3503
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3504
	/* must be updated after the train has been marked crashed */
8342
bf710180e7ce (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8334
diff changeset
  3505
	if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3506
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3507
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3508
static uint CountPassengersInTrain(const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3509
{
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  3510
	uint num = 0;
9321
61fd5b6e27ce (svn r13205) -Codechange: Remove unnecessary code-style-buggering-up macro.
peter1138
parents: 9297
diff changeset
  3511
61fd5b6e27ce (svn r13205) -Codechange: Remove unnecessary code-style-buggering-up macro.
peter1138
parents: 9297
diff changeset
  3512
	for (; v != NULL; v = v->Next()) {
7010
6f0d9f03180d (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: 7002
diff changeset
  3513
		if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) num += v->cargo.Count();
9321
61fd5b6e27ce (svn r13205) -Codechange: Remove unnecessary code-style-buggering-up macro.
peter1138
parents: 9297
diff changeset
  3514
	}
61fd5b6e27ce (svn r13205) -Codechange: Remove unnecessary code-style-buggering-up macro.
peter1138
parents: 9297
diff changeset
  3515
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3516
	return num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3517
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3518
6966
e13afa08b151 (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: 6953
diff changeset
  3519
struct TrainCollideChecker {
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3520
	Vehicle *v;  ///< vehicle we are testing for collision
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3521
	uint num;    ///< number of dead if train collided
6966
e13afa08b151 (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: 6953
diff changeset
  3522
};
e13afa08b151 (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: 6953
diff changeset
  3523
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9774
diff changeset
  3524
static Vehicle *FindTrainCollideEnum(Vehicle *v, void *data)
6966
e13afa08b151 (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: 6953
diff changeset
  3525
{
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3526
	TrainCollideChecker *tcc = (TrainCollideChecker*)data;
6966
e13afa08b151 (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: 6953
diff changeset
  3527
8281
c4b8951e7aa9 (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8271
diff changeset
  3528
	if (v->type != VEH_TRAIN) return NULL;
c4b8951e7aa9 (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8271
diff changeset
  3529
c4b8951e7aa9 (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8271
diff changeset
  3530
	/* get first vehicle now to make most usual checks faster */
c4b8951e7aa9 (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8271
diff changeset
  3531
	Vehicle *coll = v->First();
c4b8951e7aa9 (svn r11845) -Codechange: simplify train collision detection a bit
smatz
parents: 8271
diff changeset
  3532
8312
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  3533
	/* can't collide with own wagons && can't crash in depot && the same height level */
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  3534
	if (coll != tcc->v && v->u.rail.track != TRACK_BIT_DEPOT && abs(v->z_pos - tcc->v->z_pos) < 6) {
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  3535
		int x_diff = v->x_pos - tcc->v->x_pos;
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  3536
		int y_diff = v->y_pos - tcc->v->y_pos;
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  3537
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  3538
		/* needed to disable possible crash of competitor train in station by building diagonal track at its end */
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  3539
		if (x_diff * x_diff + y_diff * y_diff > 25) return NULL;
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  3540
6966
e13afa08b151 (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: 6953
diff changeset
  3541
		if (!(tcc->v->vehstatus & VS_CRASHED)) {
8312
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  3542
			/* two drivers + passengers killed in train tcc->v (if it was not crashed already) */
6966
e13afa08b151 (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: 6953
diff changeset
  3543
			tcc->num += 2 + CountPassengersInTrain(tcc->v);
e13afa08b151 (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: 6953
diff changeset
  3544
			SetVehicleCrashed(tcc->v);
e13afa08b151 (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: 6953
diff changeset
  3545
		}
e13afa08b151 (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: 6953
diff changeset
  3546
e13afa08b151 (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: 6953
diff changeset
  3547
		if (!(coll->vehstatus & VS_CRASHED)) {
e13afa08b151 (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: 6953
diff changeset
  3548
			/* two drivers + passengers killed in train coll (if it was not crashed already) */
e13afa08b151 (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: 6953
diff changeset
  3549
			tcc->num += 2 + CountPassengersInTrain(coll);
e13afa08b151 (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: 6953
diff changeset
  3550
			SetVehicleCrashed(coll);
e13afa08b151 (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: 6953
diff changeset
  3551
		}
9833
af950053ecfe (svn r13976) -Fix (r13953) [YAPP]: A crashing train could sometimes free the reservation of another train. (assisted by michi_cc)
frosch
parents: 9831
diff changeset
  3552
af950053ecfe (svn r13976) -Fix (r13953) [YAPP]: A crashing train could sometimes free the reservation of another train. (assisted by michi_cc)
frosch
parents: 9831
diff changeset
  3553
		/* Try to reserve all tiles directly under the crashed trains.
af950053ecfe (svn r13976) -Fix (r13953) [YAPP]: A crashing train could sometimes free the reservation of another train. (assisted by michi_cc)
frosch
parents: 9831
diff changeset
  3554
		 * As there might be more than two trains involved, we have to do that for all vehicles */
af950053ecfe (svn r13976) -Fix (r13953) [YAPP]: A crashing train could sometimes free the reservation of another train. (assisted by michi_cc)
frosch
parents: 9831
diff changeset
  3555
		const Vehicle *u;
af950053ecfe (svn r13976) -Fix (r13953) [YAPP]: A crashing train could sometimes free the reservation of another train. (assisted by michi_cc)
frosch
parents: 9831
diff changeset
  3556
		FOR_ALL_VEHICLES(u) {
9845
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3557
			if (u->type == VEH_TRAIN && HASBITS(u->vehstatus, VS_CRASHED) && (u->u.rail.track & TRACK_BIT_DEPOT) == TRACK_BIT_NONE) {
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3558
				TrackBits trackbits = u->u.rail.track;
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3559
				if ((trackbits & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) {
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3560
					/* Vehicle is inside a wormhole, v->u.rail.track contains no useful value then. */
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3561
					trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(u->tile));
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3562
				}
9857
8d0e4ab99439 (svn r14001) -Fix (r13989): reading wrong variable, train crash on bridge crashes game
smatz
parents: 9845
diff changeset
  3563
				TryReserveRailTrack(u->tile, TrackBitsToTrack(trackbits));
9833
af950053ecfe (svn r13976) -Fix (r13953) [YAPP]: A crashing train could sometimes free the reservation of another train. (assisted by michi_cc)
frosch
parents: 9831
diff changeset
  3564
			}
af950053ecfe (svn r13976) -Fix (r13953) [YAPP]: A crashing train could sometimes free the reservation of another train. (assisted by michi_cc)
frosch
parents: 9831
diff changeset
  3565
		}
6966
e13afa08b151 (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: 6953
diff changeset
  3566
	}
e13afa08b151 (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: 6953
diff changeset
  3567
e13afa08b151 (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: 6953
diff changeset
  3568
	return NULL;
e13afa08b151 (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: 6953
diff changeset
  3569
}
e13afa08b151 (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: 6953
diff changeset
  3570
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3571
/**
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3572
 * Checks whether the specified train has a collision with another vehicle. If
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3573
 * 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
  3574
 * 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
  3575
 * plays a sound.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3576
 */
10282
ce00edcf1112 (svn r14521) -Fix [FS#2378]: fast trains could continue to move after a crash
glx
parents: 10261
diff changeset
  3577
static bool CheckTrainCollision(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3578
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3579
	/* can't collide in depot */
10282
ce00edcf1112 (svn r14521) -Fix [FS#2378]: fast trains could continue to move after a crash
glx
parents: 10261
diff changeset
  3580
	if (v->u.rail.track == TRACK_BIT_DEPOT) return false;
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3581
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3582
	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
  3583
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3584
	TrainCollideChecker tcc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3585
	tcc.v = v;
6966
e13afa08b151 (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: 6953
diff changeset
  3586
	tcc.num = 0;
e13afa08b151 (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: 6953
diff changeset
  3587
e13afa08b151 (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: 6953
diff changeset
  3588
	/* find colliding vehicles */
7371
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  3589
	if (v->u.rail.track == TRACK_BIT_WORMHOLE) {
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  3590
		FindVehicleOnPos(v->tile, &tcc, FindTrainCollideEnum);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  3591
		FindVehicleOnPos(GetOtherTunnelBridgeEnd(v->tile), &tcc, FindTrainCollideEnum);
7371
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  3592
	} else {
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  3593
		FindVehicleOnPosXY(v->x_pos, v->y_pos, &tcc, FindTrainCollideEnum);
7371
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  3594
	}
6966
e13afa08b151 (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: 6953
diff changeset
  3595
e13afa08b151 (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: 6953
diff changeset
  3596
	/* any dead -> no crash */
10282
ce00edcf1112 (svn r14521) -Fix [FS#2378]: fast trains could continue to move after a crash
glx
parents: 10261
diff changeset
  3597
	if (tcc.num == 0) return false;
6966
e13afa08b151 (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: 6953
diff changeset
  3598
e13afa08b151 (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: 6953
diff changeset
  3599
	SetDParam(0, tcc.num);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3600
	AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL,
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9224
diff changeset
  3601
		NS_ACCIDENT_VEHICLE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3602
		v->index,
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3603
		0
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3604
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3605
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3606
	ModifyStationRatingAround(v->tile, v->owner, -160, 30);
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  3607
	SndPlayVehicleFx(SND_13_BIG_CRASH, v);
10282
ce00edcf1112 (svn r14521) -Fix [FS#2378]: fast trains could continue to move after a crash
glx
parents: 10261
diff changeset
  3608
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3609
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3610
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9774
diff changeset
  3611
static Vehicle *CheckVehicleAtSignal(Vehicle *v, void *data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3612
{
9699
33ebfa8216ca (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz
parents: 9628
diff changeset
  3613
	DiagDirection exitdir = *(DiagDirection *)data;
33ebfa8216ca (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz
parents: 9628
diff changeset
  3614
9774
190734cb1113 (svn r13911) -Fix (r13806): do not consider crashed train as waiting at signal, v->direction doesn't have to match track anyway
smatz
parents: 9738
diff changeset
  3615
	/* front engine of a train, not inside wormhole or depot, not crashed */
190734cb1113 (svn r13911) -Fix (r13806): do not consider crashed train as waiting at signal, v->direction doesn't have to match track anyway
smatz
parents: 9738
diff changeset
  3616
	if (v->type == VEH_TRAIN && IsFrontEngine(v) && (v->u.rail.track & TRACK_BIT_MASK) != 0 && !(v->vehstatus & VS_CRASHED)) {
9699
33ebfa8216ca (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz
parents: 9628
diff changeset
  3617
		if (v->cur_speed <= 5 && TrainExitDir(v->direction, v->u.rail.track) == exitdir) return v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3618
	}
9699
33ebfa8216ca (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz
parents: 9628
diff changeset
  3619
1432
768af765b5be (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
  3620
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3621
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3622
8710
cb7cf95986d9 (svn r12384) -Fix (r2428): do not disconnect train when reversing, it may 'think' it is whole in a depot
smatz
parents: 8706
diff changeset
  3623
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
  3624
{
1961
60efcaa14311 (svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents: 1942
diff changeset
  3625
	Vehicle *prev;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3626
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3627
	/* For every vehicle after and including the given vehicle */
8710
cb7cf95986d9 (svn r12384) -Fix (r2428): do not disconnect train when reversing, it may 'think' it is whole in a depot
smatz
parents: 8706
diff changeset
  3628
	for (prev = v->Previous(); v != nomove; prev = v, v = v->Next()) {
6871
d8b53c9f0b13 (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: 6857
diff changeset
  3629
		DiagDirection enterdir = DIAGDIR_BEGIN;
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3630
		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
  3631
		BeginVehicleMove(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3632
6153
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
  3633
		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3634
		if (v->u.rail.track != TRACK_BIT_WORMHOLE) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3635
			/* Not inside tunnel */
6152
6f9fbee77a47 (svn r8896) -Fix
tron
parents: 6151
diff changeset
  3636
			if (gp.old_tile == gp.new_tile) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3637
				/* Staying in the old tile */
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3638
				if (v->u.rail.track == TRACK_BIT_DEPOT) {
5994
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3639
					/* Inside depot */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3640
					gp.x = v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3641
					gp.y = v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3642
				} else {
5994
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3643
					/* Not inside depot */
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3644
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
  3645
					if (IsFrontEngine(v) && !TrainCheckIfLineEnds(v)) return;
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3646
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3647
					uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  3648
					if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3649
						goto invalid_rail;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3650
					}
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  3651
					if (HasBit(r, VETS_ENTERED_STATION)) {
5991
ec2eebfe86de (svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents: 5944
diff changeset
  3652
						TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3653
						return;
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
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8832
diff changeset
  3656
					if (v->current_order.IsType(OT_LEAVESTATION)) {
6263
6bd0726c74e2 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6259
diff changeset
  3657
						v->current_order.Free();
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8344
diff changeset
  3658
						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
  3659
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3660
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3661
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3662
				/* A new tile is about to be entered. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3663
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3664
				/* Determine what direction we're entering the new tile from */
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3665
				Direction dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
6871
d8b53c9f0b13 (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: 6857
diff changeset
  3666
				enterdir = DirToDiagDir(dir);
5994
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3667
				assert(IsValidDiagDirection(enterdir));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3668
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3669
				/* 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
  3670
				 * away the bits that aren't reachable. */
8794
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  3671
				TrackStatus ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL, 0, ReverseDiagDir(enterdir));
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  3672
				TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(enterdir);
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  3673
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  3674
				TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  3675
				TrackBits red_signals = TrackdirBitsToTrackBits(TrackStatusToRedSignals(ts) & reachable_trackdirs);
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8614
diff changeset
  3676
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8614
diff changeset
  3677
				TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  3678
				if (_settings_game.pf.pathfinder_for_trains != VPF_NTP && _settings_game.pf.forbid_90_deg && prev == NULL) {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3679
					/* We allow wagons to make 90 deg turns, because forbid_90_deg
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3680
					 * can be switched on halfway a turn */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  3681
					bits &= ~TrackCrossesTracks(FindFirstTrack(v->u.rail.track));
3017
a75caf4efa2d (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
  3682
				}
a75caf4efa2d (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
  3683
5994
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3684
				if (bits == TRACK_BIT_NONE) goto invalid_rail;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3685
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3686
				/* 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
  3687
				 * with the current train, if not, bail out. */
5994
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3688
				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
  3689
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3690
				TrackBits chosen_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3691
				if (prev == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3692
					/* 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
  3693
					 * available tracks to choose */
9810
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  3694
					chosen_track = TrackToTrackBits(ChooseTrainTrack(v, gp.new_tile, enterdir, bits, false, NULL, true));
c82ec2332a1b (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium
parents: 9809
diff changeset
  3695
					assert(chosen_track & (bits | GetReservedTrackbits(gp.new_tile)));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3696
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3697
					/* Check if it's a red signal and that force proceed is not clicked. */
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8614
diff changeset
  3698
					if (red_signals & chosen_track && v->u.rail.force_proceed == 0) {
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8614
diff changeset
  3699
						/* In front of a red signal */
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8614
diff changeset
  3700
						Trackdir i = FindFirstTrackdir(trackdirbits);
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3701
9808
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  3702
						/* Don't handle stuck trains here. */
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  3703
						if (HasBit(v->u.rail.flags, VRF_TRAIN_STUCK)) return;
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  3704
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3705
						if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) {
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3706
							v->cur_speed = 0;
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3707
							v->subspeed = 0;
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3708
							v->progress = 255 - 100;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  3709
							if (++v->load_unload_time_rem < _settings_game.pf.wait_oneway_signal * 20) return;
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3710
						} else if (HasSignalOnTrackdir(gp.new_tile, i)) {
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3711
							v->cur_speed = 0;
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3712
							v->subspeed = 0;
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6490
diff changeset
  3713
							v->progress = 255 - 10;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  3714
							if (++v->load_unload_time_rem < _settings_game.pf.wait_twoway_signal * 73) {
9699
33ebfa8216ca (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz
parents: 9628
diff changeset
  3715
								DiagDirection exitdir = TrackdirToExitdir(i);
33ebfa8216ca (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz
parents: 9628
diff changeset
  3716
								TileIndex o_tile = TileAddByDiagDir(gp.new_tile, exitdir);
33ebfa8216ca (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz
parents: 9628
diff changeset
  3717
33ebfa8216ca (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz
parents: 9628
diff changeset
  3718
								exitdir = ReverseDiagDir(exitdir);
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3719
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3720
								/* check if a train is waiting on the other side */
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  3721
								if (!HasVehicleOnPos(o_tile, &exitdir, &CheckVehicleAtSignal)) return;
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3722
							}
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3723
						}
9820
7838f660287a (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium
parents: 9819
diff changeset
  3724
7838f660287a (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium
parents: 9819
diff changeset
  3725
						/* If we would reverse but are currently in a PBS block and
7838f660287a (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium
parents: 9819
diff changeset
  3726
						 * reversing of stuck trains is disabled, don't reverse. */
7838f660287a (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium
parents: 9819
diff changeset
  3727
						if (_settings_game.pf.wait_for_pbs_path == 255 && UpdateSignalsOnSegment(v->tile, enterdir, v->owner) == SIGSEG_PBS) {
7838f660287a (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium
parents: 9819
diff changeset
  3728
							v->load_unload_time_rem = 0;
7838f660287a (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium
parents: 9819
diff changeset
  3729
							return;
7838f660287a (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium
parents: 9819
diff changeset
  3730
						}
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3731
						goto reverse_train_direction;
9888
90d3e362b95c (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium
parents: 9874
diff changeset
  3732
					} else {
90d3e362b95c (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium
parents: 9874
diff changeset
  3733
						TryReserveRailTrack(gp.new_tile, TrackBitsToTrack(chosen_track));
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  3734
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3735
				} else {
5994
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3736
					static const TrackBits _matching_tracks[8] = {
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3737
							TRACK_BIT_LEFT  | TRACK_BIT_RIGHT, TRACK_BIT_X,
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3738
							TRACK_BIT_UPPER | TRACK_BIT_LOWER, TRACK_BIT_Y,
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3739
							TRACK_BIT_LEFT  | TRACK_BIT_RIGHT, TRACK_BIT_X,
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3740
							TRACK_BIT_UPPER | TRACK_BIT_LOWER, TRACK_BIT_Y
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3741
					};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3742
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3743
					/* The wagon is active, simply follow the prev vehicle. */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  3744
					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
  3745
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3746
5994
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3747
				/* Make sure chosen track is a valid track */
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3748
				assert(
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3749
						chosen_track == TRACK_BIT_X     || chosen_track == TRACK_BIT_Y ||
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3750
						chosen_track == TRACK_BIT_UPPER || chosen_track == TRACK_BIT_LOWER ||
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
  3751
						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
  3752
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3753
				/* Update XY to reflect the entrance to the new tile, and select the direction to use */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3754
				const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3755
				gp.x = (gp.x & ~0xF) | b[0];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3756
				gp.y = (gp.y & ~0xF) | b[1];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3757
				Direction chosen_dir = (Direction)b[2];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3758
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3759
				/* Call the landscape function and tell it that the vehicle entered the tile */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3760
				uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  3761
				if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3762
					goto invalid_rail;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3763
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3764
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  3765
				if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
9809
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  3766
					Track track = FindFirstTrack(chosen_track);
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  3767
					Trackdir tdir = TrackDirectionToTrackdir(track, chosen_dir);
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  3768
					if (IsFrontEngine(v) && HasPbsSignalOnTrackdir(gp.new_tile, tdir)) {
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  3769
						SetSignalStateByTrackdir(gp.new_tile, tdir, SIGNAL_STATE_RED);
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  3770
						MarkTileDirtyByTile(gp.new_tile);
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  3771
					}
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  3772
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  3773
					/* Clear any track reservation when the last vehicle leaves the tile */
10103
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10102
diff changeset
  3774
					if (v->Next() == NULL) ClearPathReservation(v, v->tile, GetVehicleTrackdir(v));
9809
443e458de566 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium
parents: 9808
diff changeset
  3775
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3776
					v->tile = gp.new_tile;
3355
e414a0b104a6 (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
  3777
6154
8f4c65bdf930 (svn r8899) -Fix
tron
parents: 6153
diff changeset
  3778
					if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_tile)) {
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  3779
						TrainPowerChanged(v->First());
3355
e414a0b104a6 (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
  3780
					}
e414a0b104a6 (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
  3781
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3782
					v->u.rail.track = chosen_track;
1330
5d76a0522a11 (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
  3783
					assert(v->u.rail.track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3784
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3785
6871
d8b53c9f0b13 (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: 6857
diff changeset
  3786
				/* We need to update signal status, but after the vehicle position hash
d8b53c9f0b13 (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: 6857
diff changeset
  3787
				 * has been updated by AfterSetTrainPos() */
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3788
				update_signals_crossing = true;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3789
3017
a75caf4efa2d (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
  3790
				if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3791
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3792
				v->direction = chosen_dir;
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3793
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3794
				if (IsFrontEngine(v)) {
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3795
					v->load_unload_time_rem = 0;
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3796
9824
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9822
diff changeset
  3797
					/* If we are approching a crossing that is reserved, play the sound now. */
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9822
diff changeset
  3798
					TileIndex crossing = TrainApproachingCrossingTile(v);
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9822
diff changeset
  3799
					if (crossing != INVALID_TILE && GetCrossingReservation(crossing)) SndPlayTileFx(SND_0E_LEVEL_CROSSING, crossing);
4db1a1b0ab09 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium
parents: 9822
diff changeset
  3800
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3801
					/* Always try to extend the reservation when entering a tile. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3802
					CheckNextTrainTile(v);
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3803
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3804
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3805
		} else {
8143
4627396b4811 (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: 8140
diff changeset
  3806
			/* In a tunnel or on a bridge
4627396b4811 (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: 8140
diff changeset
  3807
			 * - for tunnels, only the part when the vehicle is not visible (part of enter/exit tile too)
4627396b4811 (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: 8140
diff changeset
  3808
			 * - for bridges, only the middle part - without the bridge heads */
6141
bd1637f172b4 (svn r8884) -Fix
tron
parents: 6136
diff changeset
  3809
			if (!(v->vehstatus & VS_HIDDEN)) {
bd1637f172b4 (svn r8884) -Fix
tron
parents: 6136
diff changeset
  3810
				v->cur_speed =
8491
97fcf662c3c3 (svn r12066) -Codechange: Rename GetBridge for the more common GetBridgeSpec
belugas
parents: 8482
diff changeset
  3811
					min(v->cur_speed, GetBridgeSpec(GetBridgeType(v->tile))->speed);
6141
bd1637f172b4 (svn r8884) -Fix
tron
parents: 6136
diff changeset
  3812
			}
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  3813
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3814
			if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3815
				/* Perform look-ahead on tunnel exit. */
9888
90d3e362b95c (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium
parents: 9874
diff changeset
  3816
				if (IsFrontEngine(v)) {
90d3e362b95c (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium
parents: 9874
diff changeset
  3817
					TryReserveRailTrack(gp.new_tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(gp.new_tile)));
90d3e362b95c (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium
parents: 9874
diff changeset
  3818
					CheckNextTrainTile(v);
90d3e362b95c (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium
parents: 9874
diff changeset
  3819
				}
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3820
			} else {
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3821
				v->x_pos = gp.x;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3822
				v->y_pos = gp.y;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3823
				VehiclePositionChanged(v);
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  3824
				if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3825
				continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3826
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3827
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3828
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3829
		/* update image of train, as well as delta XY */
8143
4627396b4811 (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: 8140
diff changeset
  3830
		v->UpdateDeltaXY(v->direction);
4627396b4811 (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: 8140
diff changeset
  3831
		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
  3832
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3833
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3834
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3835
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3836
		/* update the Z position of the vehicle */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3837
		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
  3838
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3839
		if (prev == NULL) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3840
			/* This is the first vehicle in the train */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3841
			AffectSpeedByZChange(v, old_z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3842
		}
6871
d8b53c9f0b13 (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: 6857
diff changeset
  3843
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3844
		if (update_signals_crossing) {
9821
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3845
			if (IsFrontEngine(v)) {
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3846
				if (TrainMovedChangeSignals(gp.new_tile, enterdir)) {
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3847
					/* We are entering a block with PBS signals right now, but
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3848
					 * not through a PBS signal. This means we don't have a
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3849
					 * reservation right now. As a conventional signal will only
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3850
					 * ever be green if no other train is in the block, getting
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3851
					 * a path should always be possible. If the player built
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3852
					 * such a strange network that it is not possible, the train
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3853
					 * will be marked as stuck and the player has to deal with
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3854
					 * the problem. */
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3855
					if ((!HasReservedTracks(gp.new_tile, v->u.rail.track) &&
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3856
							!TryReserveRailTrack(gp.new_tile, FindFirstTrack(v->u.rail.track))) ||
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3857
							!TryPathReserve(v)) {
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3858
						MarkTrainAsStuck(v);
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3859
					}
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3860
				}
8267a774aa12 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium
parents: 9820
diff changeset
  3861
			}
6871
d8b53c9f0b13 (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: 6857
diff changeset
  3862
d8b53c9f0b13 (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: 6857
diff changeset
  3863
			/* Signals can only change when the first
d8b53c9f0b13 (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: 6857
diff changeset
  3864
			 * (above) or the last vehicle moves. */
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3865
			if (v->Next() == NULL) {
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3866
				TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
8342
bf710180e7ce (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8334
diff changeset
  3867
				if (IsLevelCrossingTile(gp.old_tile)) UpdateLevelCrossing(gp.old_tile);
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3868
			}
6871
d8b53c9f0b13 (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: 6857
diff changeset
  3869
		}
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3870
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3871
		/* Do not check on every tick to save some computing time. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  3872
		if (IsFrontEngine(v) && v->tick_counter % _settings_game.pf.path_backoff_interval == 0) CheckNextTrainTile(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3873
	}
1438
98d01c6873f7 (svn r1942) Fix r1938
tron
parents: 1434
diff changeset
  3874
	return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3875
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3876
invalid_rail:
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3877
	/* We've reached end of line?? */
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9413
diff changeset
  3878
	if (prev != NULL) error("Disconnecting train");
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3879
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3880
reverse_train_direction:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3881
	v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3882
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3883
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3884
	ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3885
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3886
9840
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3887
/** Collect trackbits of all crashed train vehicles on a tile
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  3888
 * @param v Vehicle passed from Find/HasVehicleOnPos()
9840
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3889
 * @param data trackdirbits for the result
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  3890
 * @return NULL to iterate over all vehicles on the tile.
9840
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3891
 */
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3892
static Vehicle *CollectTrackbitsFromCrashedVehiclesEnum(Vehicle *v, void *data)
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3893
{
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3894
	TrackBits *trackbits = (TrackBits *)data;
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3895
9845
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3896
	if (v->type == VEH_TRAIN && (v->vehstatus & VS_CRASHED) != 0) {
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3897
		if ((v->u.rail.track & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) {
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3898
			/* Vehicle is inside a wormhole, v->u.rail.track contains no useful value then. */
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3899
			*trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(v->tile));
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3900
		} else {
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3901
			*trackbits |= v->u.rail.track;
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3902
		}
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3903
	}
9840
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3904
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3905
	return NULL;
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3906
}
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3907
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3908
/**
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3909
 * Deletes/Clears the last wagon of a crashed train. It takes the engine of the
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3910
 * train, then goes to the last wagon and deletes that. Each call to this function
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3911
 * will remove the last wagon of a crashed train. If this wagon was on a crossing,
8073
5a254e1c34e7 (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz
parents: 8072
diff changeset
  3912
 * or inside a tunnel/bridge, recalculate the signals as they might need updating
6484
79156627e1ba (svn r9665) -Documentation: Doxygen corrections,errors, corrections of corrections...
belugas
parents: 6453
diff changeset
  3913
 * @param v the Vehicle of which last wagon is to be removed
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3914
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3915
static void DeleteLastWagon(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3916
{
8267
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3917
	Vehicle *first = v->First();
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3918
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3919
	/* Go to the last wagon and delete the link pointing there
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3920
	 * *u is then the one-before-last wagon, and *v the last
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3921
	 * one which will physicially be removed */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3922
	Vehicle *u = v;
7492
35acee076719 (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: 7488
diff changeset
  3923
	for (; v->Next() != NULL; v = v->Next()) u = v;
35acee076719 (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: 7488
diff changeset
  3924
	u->SetNext(NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3925
8267
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3926
	if (first == v) {
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3927
		/* Removing front vehicle (the last to go) */
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3928
		DeleteWindowById(WC_VEHICLE_VIEW, v->index);
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3929
		InvalidateWindow(WC_COMPANY, v->owner);
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3930
	} else {
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3931
		/* Recalculate cached train properties */
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9699
diff changeset
  3932
		TrainConsistChanged(first, false);
8267
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3933
		/* Update the depot window if the first vehicle is in depot -
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3934
		 * if v == first, then it is updated in PreDestructor() */
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3935
		if (first->u.rail.track == TRACK_BIT_DEPOT) {
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3936
			InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3937
		}
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3938
	}
ea6dbc5b2148 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz
parents: 8264
diff changeset
  3939
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9274
diff changeset
  3940
	InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3941
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8312
diff changeset
  3942
	MarkSingleVehicleDirty(v);
6643
f81bee57bc09 (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: 6608
diff changeset
  3943
8255
b6be574fb424 (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz
parents: 8254
diff changeset
  3944
	/* 'v' shouldn't be accessed after it has been deleted */
9840
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3945
	TrackBits trackbits = v->u.rail.track;
8255
b6be574fb424 (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz
parents: 8254
diff changeset
  3946
	TileIndex tile = v->tile;
8300
dfd530665621 (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8283
diff changeset
  3947
	Owner owner = v->owner;
8255
b6be574fb424 (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz
parents: 8254
diff changeset
  3948
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  3949
	delete v;
9840
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3950
	v = NULL; // make sure nobody will try to read 'v' anymore
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3951
9845
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3952
	if ((trackbits & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) {
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3953
		/* Vehicle is inside a wormhole, v->u.rail.track contains no useful value then. */
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3954
		trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(tile));
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3955
	}
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3956
b1056419429f (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium
parents: 9840
diff changeset
  3957
	Track track = TrackBitsToTrack(trackbits);
9840
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3958
	if (HasReservedTracks(tile, trackbits)) {
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3959
		UnreserveRailTrack(tile, track);
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3960
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3961
		/* If there are still crashed vehicles on the tile, give the track reservation to them */
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3962
		TrackBits remaining_trackbits = TRACK_BIT_NONE;
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 9992
diff changeset
  3963
		FindVehicleOnPos(tile, &remaining_trackbits, CollectTrackbitsFromCrashedVehiclesEnum);
9840
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3964
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3965
		/* It is important that these two are the first in the loop, as reservation cannot deal with every trackbit combination */
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3966
		assert(TRACK_BEGIN == TRACK_X && TRACK_Y == TRACK_BEGIN + 1);
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3967
		for (Track t = TRACK_BEGIN; t < TRACK_END; t++) {
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3968
			if (HasBit(remaining_trackbits, t)) {
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3969
				TryReserveRailTrack(tile, t);
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3970
			}
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3971
		}
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3972
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3973
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  3974
	/* check if the wagon was on a road/rail-crossing */
8342
bf710180e7ce (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8334
diff changeset
  3975
	if (IsLevelCrossingTile(tile)) UpdateLevelCrossing(tile);
8255
b6be574fb424 (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz
parents: 8254
diff changeset
  3976
8257
3151fdbc73f9 (svn r11821) -Fix (r11802): 'optimization assert' when removing crashed wagon in some cases
smatz
parents: 8256
diff changeset
  3977
	/* Update signals */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
  3978
	if (IsTileType(tile, MP_TUNNELBRIDGE) || IsRailDepotTile(tile)) {
8300
dfd530665621 (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8283
diff changeset
  3979
		UpdateSignalsOnSegment(tile, INVALID_DIAGDIR, owner);
8257
3151fdbc73f9 (svn r11821) -Fix (r11802): 'optimization assert' when removing crashed wagon in some cases
smatz
parents: 8256
diff changeset
  3980
	} else {
9840
ac0f57d075f5 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch
parents: 9833
diff changeset
  3981
		SetSignalsOnBothDir(tile, track, owner);
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  3982
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3983
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3984
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3985
static void ChangeTrainDirRandomly(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3986
{
3160
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3987
	static const DirDiff delta[] = {
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3988
		DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3989
	};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3990
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3991
	do {
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  3992
		/* We don't need to twist around vehicles if they're not visible */
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  3993
		if (!(v->vehstatus & VS_HIDDEN)) {
3977
513433ebd092 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3994
			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
  3995
			BeginVehicleMove(v);
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  3996
			v->UpdateDeltaXY(v->direction);
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7109
diff changeset
  3997
			v->cur_image = v->GetImage(v->direction);
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  3998
			/* Refrain from updating the z position of the vehicle when on
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  3999
			 * a bridge, because AfterSetTrainPos will put the vehicle under
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  4000
			 * the bridge in that case */
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  4001
			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
  4002
		}
7492
35acee076719 (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: 7488
diff changeset
  4003
	} while ((v = v->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4004
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4005
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4006
static void HandleCrashedTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4007
{
3017
a75caf4efa2d (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
  4008
	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
  4009
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  4010
	if (state == 4 && !(v->vehstatus & VS_HIDDEN)) {
1359
52782e5cf7c9 (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
  4011
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4012
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4013
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4014
	uint32 r;
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7932
diff changeset
  4015
	if (state <= 200 && Chance16R(1, 7, r)) {
3017
a75caf4efa2d (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
  4016
		int index = (r * 10 >> 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4017
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4018
		Vehicle *u = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4019
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4020
			if (--index < 0) {
1137
bce056e230ce (svn r1638) -Fix: Train crashes should no longer desync the game. This is more of a
celestar
parents: 1132
diff changeset
  4021
				r = Random();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4022
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4023
				CreateEffectVehicleRel(u,
2140
a04d0142ad65 (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
  4024
					GB(r,  8, 3) + 2,
a04d0142ad65 (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
  4025
					GB(r, 16, 3) + 2,
a04d0142ad65 (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
  4026
					GB(r,  0, 3) + 5,
1359
52782e5cf7c9 (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
  4027
					EV_EXPLOSION_SMALL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4028
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4029
			}
7492
35acee076719 (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: 7488
diff changeset
  4030
		} while ((u = u->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4031
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4032
3017
a75caf4efa2d (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
  4033
	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
  4034
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8962
diff changeset
  4035
	if (state >= 4440 && !(v->tick_counter & 0x1F)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4036
		DeleteLastWagon(v);
6643
f81bee57bc09 (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: 6608
diff changeset
  4037
		InvalidateWindow(WC_REPLACE_VEHICLE, (v->group_id << 16) | VEH_TRAIN);
1128
ca7f860db7ac (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
  4038
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4039
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4040
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4041
static void HandleBrokenTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4042
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4043
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4044
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4045
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4046
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4047
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4048
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  4049
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4050
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4051
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  4052
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  4053
		if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  4054
			SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ?
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  4055
				SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  4056
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4057
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4058
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4059
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
9008
de19c73e405f (svn r12803) -Cleanup: rename SpecialVehicle to EffectVehicle to have a uniform naming of the thing instead of using both names for the same thing.
rubidium
parents: 9003
diff changeset
  4060
			if (u != NULL) u->u.effect.animation_state = v->breakdown_delay * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4061
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4062
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4063
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4064
	if (!(v->tick_counter & 3)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4065
		if (!--v->breakdown_delay) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4066
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4067
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4068
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4069
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4070
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4071
8283
bbe075d76da6 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz
parents: 8281
diff changeset
  4072
/** Maximum speeds for train that is broken down or approaching line end */
8312
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  4073
static const uint16 _breakdown_speeds[16] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4074
	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
  4075
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4076
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4077
8283
bbe075d76da6 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz
parents: 8281
diff changeset
  4078
/**
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4079
 * Train is approaching line end, slow down and possibly reverse
8283
bbe075d76da6 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz
parents: 8281
diff changeset
  4080
 *
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4081
 * @param v front train engine
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4082
 * @param signal not line end, just a red signal
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4083
 * @return true iff we did NOT have to reverse
8283
bbe075d76da6 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz
parents: 8281
diff changeset
  4084
 */
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4085
static bool TrainApproachingLineEnd(Vehicle *v, bool signal)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4086
{
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4087
	/* Calc position within the current tile */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4088
	uint x = v->x_pos & 0xF;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4089
	uint y = v->y_pos & 0xF;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  4090
8312
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  4091
	/* for diagonal directions, 'x' will be 0..15 -
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  4092
	 * for other directions, it will be 1, 3, 5, ..., 15 */
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  4093
	switch (v->direction) {
8312
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  4094
		case DIR_N : x = ~x + ~y + 25; break;
3186
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  4095
		case DIR_NW: x = y;            /* FALLTHROUGH */
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  4096
		case DIR_NE: x = ~x + 16;      break;
8312
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  4097
		case DIR_E : x = ~x + y + 9;   break;
3186
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  4098
		case DIR_SE: x = y;            break;
8312
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  4099
		case DIR_S : x = x + y - 7;    break;
8b4944b3b5a3 (svn r11877) -Fix: another way to crash competitors' train in a station
smatz
parents: 8305
diff changeset
  4100
		case DIR_W : x = ~y + x + 9;   break;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  4101
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4102
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4103
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4104
	/* do not reverse when approaching red signal */
10287
e84ee4b8ba7b (svn r14526) -Fix [FS#2379]: make sure trains stop at the end of a station; a 3/8th length train did stop 2/8th of it's length too early causing a 63/8th long train not to fit in a 4 tile station.
rubidium
parents: 10282
diff changeset
  4105
	if (!signal && x + (v->u.rail.cached_veh_length + 1) / 2 >= TILE_SIZE) {
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4106
		/* 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
  4107
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4108
		ReverseTrainDirection(v);
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  4109
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4110
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4111
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  4112
	/* slow down */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4113
	v->vehstatus |= VS_TRAIN_SLOWING;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4114
	uint16 break_speed = _breakdown_speeds[x & 0xF];
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  4115
	if (break_speed < v->cur_speed) v->cur_speed = break_speed;
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  4116
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  4117
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4118
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4119
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4120
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4121
/**
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4122
 * Determines whether train would like to leave the tile
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4123
 * @param v train to test
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4124
 * @return true iff vehicle is NOT entering or inside a depot or tunnel/bridge
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4125
 */
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4126
static bool TrainCanLeaveTile(const Vehicle *v)
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4127
{
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4128
	/* Exit if inside a tunnel/bridge or a depot */
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4129
	if (v->u.rail.track == TRACK_BIT_WORMHOLE || v->u.rail.track == TRACK_BIT_DEPOT) return false;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4130
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4131
	TileIndex tile = v->tile;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4132
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4133
	/* entering a tunnel/bridge? */
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4134
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4135
		DiagDirection dir = GetTunnelBridgeDirection(tile);
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4136
		if (DiagDirToDir(dir) == v->direction) return false;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4137
	}
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4138
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4139
	/* entering a depot? */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
  4140
	if (IsRailDepotTile(tile)) {
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4141
		DiagDirection dir = ReverseDiagDir(GetRailDepotDirection(tile));
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4142
		if (DiagDirToDir(dir) == v->direction) return false;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4143
	}
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4144
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4145
	return true;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4146
}
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4147
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4148
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4149
/**
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4150
 * Determines whether train is approaching a rail-road crossing
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4151
 *   (thus making it barred)
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4152
 * @param v front engine of train
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4153
 * @return TileIndex of crossing the train is approaching, else INVALID_TILE
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4154
 * @pre v in non-crashed front engine
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4155
 */
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4156
static TileIndex TrainApproachingCrossingTile(const Vehicle *v)
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4157
{
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4158
	assert(IsFrontEngine(v));
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4159
	assert(!(v->vehstatus & VS_CRASHED));
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4160
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4161
	if (!TrainCanLeaveTile(v)) return INVALID_TILE;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4162
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4163
	DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track);
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4164
	TileIndex tile = v->tile + TileOffsByDiagDir(dir);
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4165
8606
24fd4e7af185 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz
parents: 8598
diff changeset
  4166
	/* not a crossing || wrong axis || unusable rail (wrong type or owner) */
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4167
	if (!IsLevelCrossingTile(tile) || DiagDirToAxis(dir) == GetCrossingRoadAxis(tile) ||
8606
24fd4e7af185 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz
parents: 8598
diff changeset
  4168
			!CheckCompatibleRail(v, tile)) {
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4169
		return INVALID_TILE;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4170
	}
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4171
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4172
	return tile;
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4173
}
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4174
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4175
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4176
/**
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4177
 * Checks for line end. Also, bars crossing at next tile if needed
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4178
 *
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4179
 * @param v vehicle we are checking
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4180
 * @return true iff we did NOT have to reverse
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4181
 */
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4182
static bool TrainCheckIfLineEnds(Vehicle *v)
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4183
{
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4184
	/* First, handle broken down train */
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4185
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4186
	int t = v->breakdown_ctr;
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4187
	if (t > 1) {
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4188
		v->vehstatus |= VS_TRAIN_SLOWING;
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4189
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4190
		uint16 break_speed = _breakdown_speeds[GB(~t, 4, 4)];
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4191
		if (break_speed < v->cur_speed) v->cur_speed = break_speed;
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4192
	} else {
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4193
		v->vehstatus &= ~VS_TRAIN_SLOWING;
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4194
	}
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4195
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4196
	if (!TrainCanLeaveTile(v)) return true;
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4197
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4198
	/* Determine the non-diagonal direction in which we will exit this tile */
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4199
	DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track);
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4200
	/* Calculate next tile */
8334
9c2b712806a5 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz
parents: 8317
diff changeset
  4201
	TileIndex tile = v->tile + TileOffsByDiagDir(dir);
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4202
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4203
	/* Determine the track status on the next tile */
8794
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  4204
	TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0, ReverseDiagDir(dir));
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  4205
	TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(dir);
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  4206
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  4207
	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
4a3ee468d97a (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch
parents: 8793
diff changeset
  4208
	TrackdirBits red_signals = TrackStatusToRedSignals(ts) & reachable_trackdirs;
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4209
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4210
	/* We are sure the train is not entering a depot, it is detected above */
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4211
8482
4552a238cd58 (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz
parents: 8467
diff changeset
  4212
	/* mask unreachable track bits if we are forbidden to do 90deg turns */
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8614
diff changeset
  4213
	TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  4214
	if (_settings_game.pf.pathfinder_for_trains != VPF_NTP && _settings_game.pf.forbid_90_deg) {
8482
4552a238cd58 (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz
parents: 8467
diff changeset
  4215
		bits &= ~TrackCrossesTracks(FindFirstTrack(v->u.rail.track));
4552a238cd58 (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz
parents: 8467
diff changeset
  4216
	}
4552a238cd58 (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz
parents: 8467
diff changeset
  4217
8606
24fd4e7af185 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz
parents: 8598
diff changeset
  4218
	/* no suitable trackbits at all || unusable rail (wrong type or owner) */
24fd4e7af185 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz
parents: 8598
diff changeset
  4219
	if (bits == TRACK_BIT_NONE || !CheckCompatibleRail(v, tile)) {
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4220
		return TrainApproachingLineEnd(v, false);
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4221
	}
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4222
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4223
	/* approaching red signal */
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8614
diff changeset
  4224
	if ((trackdirbits & red_signals) != 0) return TrainApproachingLineEnd(v, true);
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4225
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4226
	/* approaching a rail/road crossing? then make it red */
8356
683208b00b92 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz
parents: 8350
diff changeset
  4227
	if (IsLevelCrossingTile(tile)) MaybeBarCrossingWithSound(tile);
8305
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4228
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4229
	return true;
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4230
}
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4231
6191852ae2a2 (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz
parents: 8302
diff changeset
  4232
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4233
static void TrainLocoHandler(Vehicle *v, bool mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4234
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4235
	/* train has crashed? */
7476
f6eb56e8b8a7 (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: 7425
diff changeset
  4236
	if (v->vehstatus & VS_CRASHED) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4237
		if (!mode) HandleCrashedTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4238
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4239
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4240
9808
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  4241
	if (v->u.rail.force_proceed != 0) {
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  4242
		v->u.rail.force_proceed--;
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  4243
		ClrBit(v->u.rail.flags, VRF_TRAIN_STUCK);
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  4244
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  4245
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4246
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4247
	/* train is broken down? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4248
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4249
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4250
			HandleBrokenTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4251
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4252
		}
9000
0eee16b3074d (svn r12795) -Fix [FS#1938]: vehicles could break down during loading and keep loading. The intention of the break down code is not to break down when having zero speed, therefor break downs now do not happen when loading.
rubidium
parents: 8969
diff changeset
  4253
		if (!v->current_order.IsType(OT_LOADING)) v->breakdown_ctr--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4254
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4255
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  4256
	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
  4257
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4258
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4259
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4260
	/* exit if train is stopped */
3017
a75caf4efa2d (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
  4261
	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
  4262
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  4263
	bool valid_order = v->current_order.IsValid() && v->current_order.GetType() != OT_CONDITIONAL;
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8794
diff changeset
  4264
	if (ProcessOrders(v) && CheckReverseTrain(v)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4265
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4266
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4267
		v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4268
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4269
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4270
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4271
6594
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6593
diff changeset
  4272
	v->HandleLoading(mode);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4273
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8832
diff changeset
  4274
	if (v->current_order.IsType(OT_LOADING)) return;
3017
a75caf4efa2d (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
  4275
a75caf4efa2d (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
  4276
	if (CheckTrainStayInDepot(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4277
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4278
	if (!mode) HandleLocomotiveSmokeCloud(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4279
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  4280
	/* We had no order but have an order now, do look ahead. */
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  4281
	if (!valid_order && v->current_order.IsValid()) {
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  4282
		CheckNextTrainTile(v);
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  4283
	}
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  4284
9813
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4285
	/* Handle stuck trains. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4286
	if (!mode && HasBit(v->u.rail.flags, VRF_TRAIN_STUCK)) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4287
		++v->load_unload_time_rem;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4288
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4289
		/* Should we try reversing this tick if still stuck? */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4290
		bool turn_around = v->load_unload_time_rem % (_settings_game.pf.wait_for_pbs_path * DAY_TICKS) == 0 && _settings_game.pf.wait_for_pbs_path < 255;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4291
9816
662aeec193e5 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium
parents: 9813
diff changeset
  4292
		if (!turn_around && v->load_unload_time_rem % _settings_game.pf.path_backoff_interval != 0 && v->u.rail.force_proceed == 0) return;
9813
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4293
		if (!TryPathReserve(v)) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4294
			/* Still stuck. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4295
			if (turn_around) ReverseTrainDirection(v);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4296
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4297
			if (HasBit(v->u.rail.flags, VRF_TRAIN_STUCK) && v->load_unload_time_rem > 2 * _settings_game.pf.wait_for_pbs_path * DAY_TICKS) {
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4298
				/* Show message to player. */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
  4299
				if (_settings_client.gui.lost_train_warn && v->owner == _local_company) {
9813
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4300
					SetDParam(0, v->unitnumber);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4301
					AddNewsItem(
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4302
						STR_TRAIN_IS_STUCK,
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4303
						NS_ADVICE,
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4304
						v->index,
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4305
						0);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4306
				}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4307
				v->load_unload_time_rem = 0;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4308
			}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4309
			/* Exit if force proceed not pressed, else reset stuck flag anyway. */
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4310
			if (v->u.rail.force_proceed == 0) return;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4311
			ClrBit(v->u.rail.flags, VRF_TRAIN_STUCK);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4312
			v->load_unload_time_rem = 0;
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4313
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4314
		}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4315
	}
b3d5aec7bb33 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium
parents: 9812
diff changeset
  4316
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4317
	int j = UpdateTrainSpeed(v);
8426
dcc4a852de3c (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8390
diff changeset
  4318
dcc4a852de3c (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8390
diff changeset
  4319
	/* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */
dcc4a852de3c (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8390
diff changeset
  4320
	if (v->cur_speed == 0 && v->u.rail.last_speed == 0 && v->vehstatus & VS_STOPPED) {
dcc4a852de3c (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8390
diff changeset
  4321
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
dcc4a852de3c (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8390
diff changeset
  4322
	}
dcc4a852de3c (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz
parents: 8390
diff changeset
  4323
10214
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4324
	int adv_spd = (v->direction & 1) ? 192 : 256;
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4325
	if (j < adv_spd) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  4326
		/* if the vehicle has speed 0, update the last_speed field. */
10214
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4327
		if (v->cur_speed == 0) SetLastSpeed(v, v->cur_speed);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4328
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4329
		TrainCheckIfLineEnds(v);
10214
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4330
		/* Loop until the train has finished moving. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4331
		do {
10214
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4332
			j -= adv_spd;
8710
cb7cf95986d9 (svn r12384) -Fix (r2428): do not disconnect train when reversing, it may 'think' it is whole in a depot
smatz
parents: 8706
diff changeset
  4333
			TrainController(v, NULL, true);
10282
ce00edcf1112 (svn r14521) -Fix [FS#2378]: fast trains could continue to move after a crash
glx
parents: 10261
diff changeset
  4334
			/* Don't continue to move if the train crashed. */
ce00edcf1112 (svn r14521) -Fix [FS#2378]: fast trains could continue to move after a crash
glx
parents: 10261
diff changeset
  4335
			if (CheckTrainCollision(v)) break;
10214
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4336
			/* 192 spd used for going straight, 256 for going diagonally. */
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4337
			adv_spd = (v->direction & 1) ? 192 : 256;
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4338
		} while (j >= adv_spd);
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4339
		SetLastSpeed(v, v->cur_speed);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4340
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4341
10214
15e0ca383223 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium
parents: 10208
diff changeset
  4342
	if (v->progress == 0) v->progress = j;	// Save unused spd for next time, if TrainController didn't set progress
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4343
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4344
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4345
7488
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4346
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4347
Money Train::GetRunningCost() const
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4348
{
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4349
	Money cost = 0;
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4350
	const Vehicle *v = this;
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4351
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4352
	do {
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4353
		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4354
8622
75554fef1333 (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: 8620
diff changeset
  4355
		byte cost_factor = GetVehicleProperty(v, 0x0D, rvi->running_cost);
7488
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4356
		if (cost_factor == 0) continue;
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4357
9208
191eb01c025a (svn r13074) -Fix: For multiheaded engines, halve power and running cost when used instead of when loading, to allow callback values to work properly.
peter1138
parents: 9115
diff changeset
  4358
		/* Halve running cost for multiheaded parts */
191eb01c025a (svn r13074) -Fix: For multiheaded engines, halve power and running cost when used instead of when loading, to allow callback values to work properly.
peter1138
parents: 9115
diff changeset
  4359
		if (IsMultiheaded(v)) cost_factor /= 2;
191eb01c025a (svn r13074) -Fix: For multiheaded engines, halve power and running cost when used instead of when loading, to allow callback values to work properly.
peter1138
parents: 9115
diff changeset
  4360
8626
9781464622be (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: 8622
diff changeset
  4361
		cost += cost_factor * GetPriceByIndex(rvi->running_cost_class);
7488
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4362
	} while ((v = GetNextVehicle(v)) != NULL);
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4363
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4364
	return cost;
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4365
}
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4366
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  4367
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4368
void Train::Tick()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4369
{
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4370
	if (_age_cargo_skip_counter == 0) this->cargo.AgeCargo();
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4371
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4372
	this->tick_counter++;
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4373
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4374
	if (IsFrontEngine(this)) {
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  4375
		if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4376
		this->current_order_time++;
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4377
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4378
		TrainLocoHandler(this, false);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  4379
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  4380
		/* make sure vehicle wasn't deleted. */
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4381
		if (this->type == VEH_TRAIN && IsFrontEngine(this))
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4382
			TrainLocoHandler(this, true);
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  4383
	} else if (IsFreeWagon(this) && HASBITS(this->vehstatus, VS_CRASHED)) {
7696
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
  4384
		/* Delete flooded standalone wagon chain */
ecb9955c0a4a (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium
parents: 7695
diff changeset
  4385
		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
  4386
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4387
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4388
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4389
static void CheckIfTrainNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4390
{
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  4391
	static const uint MAX_ACCEPTABLE_DEPOT_DIST = 16;
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  4392
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
  4393
	if (_settings_game.vehicle.servint_trains == 0 || !v->NeedsAutomaticServicing()) return;
7502
f821f134ec5a (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium
parents: 7497
diff changeset
  4394
	if (v->IsInDepot()) {
4529
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  4395
		VehicleServiceInDepot(v);
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  4396
		return;
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  4397
	}
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  4398
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4399
	TrainFindDepotData tfdd = FindClosestTrainDepot(v, MAX_ACCEPTABLE_DEPOT_DIST);
308
8088f72d10f5 (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
  4400
	/* Only go to the depot if it is not too far out of our way. */
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10214
diff changeset
  4401
	if (tfdd.best_length == UINT_MAX || tfdd.best_length > MAX_ACCEPTABLE_DEPOT_DIST) {
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8832
diff changeset
  4402
		if (v->current_order.IsType(OT_GOTO_DEPOT)) {
308
8088f72d10f5 (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
  4403
			/* If we were already heading for a depot but it has
8088f72d10f5 (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
  4404
			 * suddenly moved farther away, we continue our normal
8088f72d10f5 (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
  4405
			 * schedule? */
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8832
diff changeset
  4406
			v->current_order.MakeDummy();
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8344
diff changeset
  4407
			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
  4408
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4409
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4410
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4411
8850
415cd31f7e42 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz
parents: 8843
diff changeset
  4412
	const Depot *depot = GetDepotByTile(tfdd.tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4413
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8832
diff changeset
  4414
	if (v->current_order.IsType(OT_GOTO_DEPOT) &&
8840
332412c2e9c1 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 8836
diff changeset
  4415
			v->current_order.GetDestination() != depot->index &&
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7932
diff changeset
  4416
			!Chance16(3, 16)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4417
		return;
3017
a75caf4efa2d (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
  4418
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4419
8853
3266f0374302 (svn r12615) -Codechange: rename some enums related to depot orders to make it more clear that they are no loading/unloading flags. Also add more type strictness.
rubidium
parents: 8850
diff changeset
  4420
	v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
308
8088f72d10f5 (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
  4421
	v->dest_tile = tfdd.tile;
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8344
diff changeset
  4422
	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
  4423
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4424
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4425
void Train::OnNewDay()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4426
{
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4427
	if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4428
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4429
	if (IsFrontEngine(this)) {
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4430
		CheckVehicleBreakdown(this);
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4431
		AgeVehicle(this);
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4432
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4433
		CheckIfTrainNeedsService(this);
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4434
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4435
		CheckOrders(this);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  4436
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4437
		/* update destination */
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8832
diff changeset
  4438
		if (this->current_order.IsType(OT_GOTO_STATION)) {
8840
332412c2e9c1 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 8836
diff changeset
  4439
			TileIndex tile = GetStation(this->current_order.GetDestination())->train_tile;
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4440
			if (tile != 0) this->dest_tile = tile;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  4441
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4442
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  4443
		if (this->running_ticks != 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4444
			/* running costs */
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  4445
			CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (364 * DAY_TICKS));
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  4446
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  4447
			this->profit_this_year -= cost.GetCost();
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  4448
			this->running_ticks = 0;
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4449
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
  4450
			SubtractMoneyFromCompanyFract(this->owner, cost);
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4451
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4452
			InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  4453
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4454
		}
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4455
	} else if (IsTrainEngine(this)) {
7024
918963b35c5f (svn r10288) -Fix [FS#202]: also age engines that aren't front-engines (based on a patch by kaan)
truelight
parents: 7023
diff changeset
  4456
		/* Also age engines that aren't front engines */
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8426
diff changeset
  4457
		AgeVehicle(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4458
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4459
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4460
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  4461
void TrainsYearlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4462
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4463
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4464
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4465
	FOR_ALL_VEHICLES(v) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  4466
		if (v->type == VEH_TRAIN && IsFrontEngine(v)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  4467
			/* show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list) */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10192
diff changeset
  4468
			if (_settings_client.gui.train_income_warn && v->owner == _local_company && v->age >= 730 && v->GetDisplayProfitThisYear() < 0) {
8614
4c67a883c4c4 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz
parents: 8612
diff changeset
  4469
				SetDParam(1, v->GetDisplayProfitThisYear());
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  4470
				SetDParam(0, v->unitnumber);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4471
				AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4472
					STR_TRAIN_IS_UNPROFITABLE,
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9224
diff changeset
  4473
					NS_ADVICE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4474
					v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4475
					0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4476
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4477
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4478
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4479
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4480
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4481
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4482
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4483
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4484
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4485
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  4486
void InitializeTrains()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4487
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4488
	_age_cargo_skip_counter = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  4489
}
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4490
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4491
/*
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4492
 * Link front and rear multiheaded engines to each other
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4493
 * This is done when loading a savegame
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4494
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  4495
void ConnectMultiheadedTrains()
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4496
{
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4497
	Vehicle *v;
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4498
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4499
	FOR_ALL_VEHICLES(v) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  4500
		if (v->type == VEH_TRAIN) {
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4501
			v->u.rail.other_multiheaded_part = NULL;
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4502
		}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4503
	}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4504
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4505
	FOR_ALL_VEHICLES(v) {
10173
a5b026c5f1af (svn r14366) -Fix (r14362): Match multiheaded parts starting from the first vehicle in a chain.
frosch
parents: 10170
diff changeset
  4506
		if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))) {
10170
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4507
			/* Two ways to associate multiheaded parts to each other:
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4508
			 * sequential-matching: Trains shall be arranged to look like <..>..<..>..<..>..
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4509
			 * bracket-matching:    Free vehicle chains shall be arranged to look like ..<..<..>..<..>..>..
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4510
			 *
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4511
			 * Note: Old savegames might contain chains which do not comply with these rules, e.g.
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4512
			 *   - the front and read parts have invalid orders
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4513
			 *   - different engine types might be combined
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4514
			 *   - there might be different amounts of front and rear parts.
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4515
			 *
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4516
			 * Note: The multiheaded parts need to be matched exactly like they are matched on the server, else desyncs will occur.
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4517
			 *   This is why two matching strategies are needed.
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4518
			 */
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4519
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4520
			bool sequential_matching = IsFrontEngine(v);
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4521
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4522
			for (Vehicle *u = v; u != NULL; u = GetNextVehicle(u)) {
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4523
				if (u->u.rail.other_multiheaded_part != NULL) continue; // we already linked this one
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4524
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4525
				if (IsMultiheaded(u)) {
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4526
					if (!IsTrainEngine(u)) {
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4527
						/* we got a rear car without a front car. We will convert it to a front one */
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4528
						SetTrainEngine(u);
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4529
						u->spritenum--;
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4530
					}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4531
10170
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4532
					/* Find a matching back part */
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4533
					EngineID eid = u->engine_type;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4534
					Vehicle *w;
10170
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4535
					if (sequential_matching) {
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4536
						for (w = GetNextVehicle(u); w != NULL; w = GetNextVehicle(w)) {
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4537
							if (w->engine_type != eid || w->u.rail.other_multiheaded_part != NULL || !IsMultiheaded(w)) continue;
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4538
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4539
							/* we found a car to partner with this engine. Now we will make sure it face the right way */
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4540
							if (IsTrainEngine(w)) {
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4541
								ClearTrainEngine(w);
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4542
								w->spritenum++;
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4543
							}
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4544
							break;
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4545
						}
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4546
					} else {
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4547
						uint stack_pos = 0;
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4548
						for (w = GetNextVehicle(u); w != NULL; w = GetNextVehicle(w)) {
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4549
							if (w->engine_type != eid || w->u.rail.other_multiheaded_part != NULL || !IsMultiheaded(w)) continue;
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4550
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4551
							if (IsTrainEngine(w)) {
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4552
								stack_pos++;
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4553
							} else {
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4554
								if (stack_pos == 0) break;
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4555
								stack_pos--;
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4556
							}
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4557
						}
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4558
					}
65c0029018d0 (svn r14362) -Fix (r3403): Also connect multiheaded parts in free wagon chains.
frosch
parents: 10123
diff changeset
  4559
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4560
					if (w != NULL) {
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4561
						w->u.rail.other_multiheaded_part = u;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4562
						u->u.rail.other_multiheaded_part = w;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4563
					} else {
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4564
						/* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  4565
						ClearMultiheaded(u);
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4566
					}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4567
				}
9321
61fd5b6e27ce (svn r13205) -Codechange: Remove unnecessary code-style-buggering-up macro.
peter1138
parents: 9297
diff changeset
  4568
			}
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4569
		}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4570
	}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4571
}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4572
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  4573
/**
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4574
 *  Converts all trains to the new subtype format introduced in savegame 16.2
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4575
 *  It also links multiheaded engines or make them forget they are multiheaded if no suitable partner is found
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4576
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  4577
void ConvertOldMultiheadToNew()
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4578
{
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4579
	Vehicle *v;
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4580
	FOR_ALL_VEHICLES(v) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  4581
		if (v->type == VEH_TRAIN) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  4582
			SetBit(v->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4583
		}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4584
	}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4585
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4586
	FOR_ALL_VEHICLES(v) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  4587
		if (v->type == VEH_TRAIN) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  4588
			if (HasBit(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) {
9321
61fd5b6e27ce (svn r13205) -Codechange: Remove unnecessary code-style-buggering-up macro.
peter1138
parents: 9297
diff changeset
  4589
				for (Vehicle *u = v; u != NULL; u = u->Next()) {
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4590
					const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
3017
a75caf4efa2d (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
  4591
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  4592
					ClrBit(u->subtype, 7);
3017
a75caf4efa2d (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
  4593
					switch (u->subtype) {
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  4594
						case 0: /* TS_Front_Engine */
5868
94430141c189 (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: 5858
diff changeset
  4595
							if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
3017
a75caf4efa2d (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
  4596
							SetFrontEngine(u);
a75caf4efa2d (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
  4597
							SetTrainEngine(u);
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4598
							break;
3017
a75caf4efa2d (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
  4599
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  4600
						case 1: /* TS_Artic_Part */
3017
a75caf4efa2d (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
  4601
							u->subtype = 0;
a75caf4efa2d (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
  4602
							SetArticulatedPart(u);
a75caf4efa2d (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
  4603
							break;
a75caf4efa2d (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
  4604
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  4605
						case 2: /* TS_Not_First */
3017
a75caf4efa2d (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
  4606
							u->subtype = 0;
5868
94430141c189 (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: 5858
diff changeset
  4607
							if (rvi->railveh_type == RAILVEH_WAGON) {
3017
a75caf4efa2d (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
  4608
								// normal wagon
a75caf4efa2d (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
  4609
								SetTrainWagon(u);
a75caf4efa2d (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
  4610
								break;
a75caf4efa2d (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
  4611
							}
5868
94430141c189 (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: 5858
diff changeset
  4612
							if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4613
								// rear end of a multiheaded engine
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4614
								SetMultiheaded(u);
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4615
								break;
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4616
							}
5868
94430141c189 (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: 5858
diff changeset
  4617
							if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4618
							SetTrainEngine(u);
3017
a75caf4efa2d (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
  4619
							break;
a75caf4efa2d (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
  4620
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4432
diff changeset
  4621
						case 4: /* TS_Free_Car */
3017
a75caf4efa2d (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
  4622
							u->subtype = 0;
a75caf4efa2d (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
  4623
							SetTrainWagon(u);
a75caf4efa2d (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
  4624
							SetFreeWagon(u);
a75caf4efa2d (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
  4625
							break;
a75caf4efa2d (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
  4626
						default: NOT_REACHED(); break;
a75caf4efa2d (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
  4627
					}
9321
61fd5b6e27ce (svn r13205) -Codechange: Remove unnecessary code-style-buggering-up macro.
peter1138
parents: 9297
diff changeset
  4628
				}
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4629
			}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4630
		}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4631
	}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  4632
}