src/train_cmd.cpp
author rubidium
Wed, 26 Dec 2007 11:45:43 +0000
changeset 8139 4e91c448c409
parent 8131 160939e24ed3
child 8140 0d0d8c94f84b
permissions -rw-r--r--
(svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
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
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
     3
/** @file train_cmd.cpp */
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"
507
04b5403aaf6b (svn r815) Include strings.h only in the files which need it.
tron
parents: 495
diff changeset
    13
#include "table/strings.h"
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
    14
#include "tunnel_map.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "vehicle.h"
6980
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6966
diff changeset
    16
#include "timetable.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
    17
#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
    18
#include "command_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#include "pathfind.h"
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    20
#include "npf.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#include "station.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
#include "table/train_cmd.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
#include "engine.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
#include "player.h"
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
    26
#include "sound.h"
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
    27
#include "depot.h"
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1531
diff changeset
    28
#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
    29
#include "vehicle_gui.h"
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
    30
#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
    31
#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
    32
#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
    33
#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
    34
#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
    35
#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
    36
#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
    37
#include "yapf/yapf.h"
4261
28670f743746 (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4252
diff changeset
    38
#include "date.h"
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
    39
#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
    40
#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
    41
#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
    42
#include "tunnelbridge_map.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8108
diff changeset
    43
#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
    44
#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
    45
#include "window_func.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
    46
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
    48
static bool TrainCheckIfLineEnds(Vehicle *v);
5252
d4b78248de25 (svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents: 5215
diff changeset
    49
static void TrainController(Vehicle *v, bool update_image);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
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
    51
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
    52
static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
    53
static const TrackBits _state_dir_table[4] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER };
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
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
    55
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
    56
/** 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
    57
 * @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
    58
 * @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
    59
 */
5316
27fa85736f8a (svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents: 5303
diff changeset
    60
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
    61
{
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
    62
	if (!GetCargo(cargo)->is_freight) return 1;
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
    63
	return _patches.freight_trains;
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
    64
}
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
    65
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
    66
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
    67
/**
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    68
 * 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
    69
 * @param v First vehicle of the consist.
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    70
 */
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    71
void TrainPowerChanged(Vehicle* v)
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    72
{
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
    73
	uint32 total_power = 0;
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    74
	uint32 max_te = 0;
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    75
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
    76
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    77
		/* Power is not added for articulated parts */
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    78
		if (IsArticulatedPart(u)) continue;
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    79
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
    80
		RailType railtype = GetRailType(u->tile);
5769
ff59c9fcbc41 (svn r8320) -Fix
tron
parents: 5733
diff changeset
    81
		bool engine_has_power = HasPowerOnRail(u->u.rail.railtype, railtype);
ff59c9fcbc41 (svn r8320) -Fix
tron
parents: 5733
diff changeset
    82
		bool wagon_has_power  = HasPowerOnRail(v->u.rail.railtype, railtype);
ff59c9fcbc41 (svn r8320) -Fix
tron
parents: 5733
diff changeset
    83
ff59c9fcbc41 (svn r8320) -Fix
tron
parents: 5733
diff changeset
    84
		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    85
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
    86
		if (engine_has_power) {
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
    87
			uint16 power = GetVehicleProperty(u, 0x0B, rvi_u->power);
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
    88
			if (power != 0) {
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
    89
				total_power += power;
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
    90
				/* Tractive effort in (tonnes * 1000 * 10 =) N */
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
    91
				max_te += (u->u.rail.cached_veh_weight * 10000 * GetVehicleProperty(u, 0x1F, rvi_u->tractive_effort)) / 256;
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
    92
			}
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    93
		}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    94
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
    95
		if (HasBit(u->u.rail.flags, VRF_POWEREDWAGON) && (wagon_has_power)) {
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
    96
			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
    97
		}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    98
	}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
    99
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
   100
	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
   101
		/* 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
   102
		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
   103
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
   104
		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
   105
		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
   106
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   107
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   108
	}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   109
}
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   110
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   111
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   112
/**
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
   113
 * 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
   114
 * 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
   115
 * @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
   116
 */
2752
55e04dee346d (svn r3297) Staticise
tron
parents: 2704
diff changeset
   117
static void TrainCargoChanged(Vehicle* v)
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   118
{
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
   119
	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
   120
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
   121
	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
   122
		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
   123
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   124
		/* 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
   125
		if (!IsArticulatedPart(u)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   126
			/* 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
   127
			vweight += GetVehicleProperty(u, 0x16, RailVehInfo(u->engine_type)->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
   128
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   129
			/* powered wagons have extra weight added */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   130
			if (HasBit(u->u.rail.flags, VRF_POWEREDWAGON))
4017
62f8877d1085 (svn r5240) - NewGRF: when there is more than one engine in a consist, base livery overrides and powered wagon weight/power on the last engine before the wagon.
peter1138
parents: 3988
diff changeset
   131
				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
   132
		}
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
   133
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   134
		/* 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
   135
		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
   136
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   137
		/* 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
   138
		u->u.rail.cached_veh_weight = vweight;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   139
	}
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
   140
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   141
	/* 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
   142
	v->u.rail.cached_weight = weight;
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   143
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   144
	/* 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
   145
	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
   146
}
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
   147
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
   148
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
   149
/**
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
   150
 * 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
   151
 * 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
   152
 * 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
   153
 * @param v First vehicle of the chain.
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
   154
 */
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   155
void TrainConsistChanged(Vehicle* v)
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   156
{
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
   157
	uint16 max_speed = 0xFFFF;
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
   158
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
   159
	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
   160
	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
   161
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   162
	const RailVehicleInfo *rvi_v = RailVehInfo(v->engine_type);
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   163
	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
   164
	v->u.rail.cached_total_length = 0;
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
   165
	v->u.rail.compatible_railtypes = 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
   166
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
   167
	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
   168
		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
   169
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   170
		/* 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
   171
		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
   172
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   173
		/* update the 'first engine' */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   174
		u->u.rail.first_engine = v == u ? INVALID_ENGINE : first_engine;
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
   175
		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
   176
4017
62f8877d1085 (svn r5240) - NewGRF: when there is more than one engine in a consist, base livery overrides and powered wagon weight/power on the last engine before the wagon.
peter1138
parents: 3988
diff changeset
   177
		if (IsTrainEngine(u)) first_engine = u->engine_type;
62f8877d1085 (svn r5240) - NewGRF: when there is more than one engine in a consist, base livery overrides and powered wagon weight/power on the last engine before the wagon.
peter1138
parents: 3988
diff changeset
   178
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
   179
		/* 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
   180
		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
   181
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
   182
		/* 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
   183
		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
   184
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   185
		if (rvi_u->visual_effect != 0) {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   186
			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
   187
		} else {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   188
			if (IsTrainWagon(u) || IsArticulatedPart(u)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   189
				/* 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
   190
				u->u.rail.cached_vis_effect = 0x40;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   191
			} else if (rvi_u->engclass == 0) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   192
				/* Steam is offset by -4 units */
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   193
				u->u.rail.cached_vis_effect = 4;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   194
			} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   195
				/* 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
   196
				u->u.rail.cached_vis_effect = 8;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   197
			}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   198
		}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   199
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   200
		if (!IsArticulatedPart(u)) {
3926
ea737ba47ba0 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   201
			/* Check powered wagon / visual effect callback */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   202
			if (HasBit(EngInfo(u->engine_type)->callbackmask, CBM_TRAIN_WAGON_POWER)) {
3926
ea737ba47ba0 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   203
				uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, u->engine_type, u);
ea737ba47ba0 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   204
ea737ba47ba0 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   205
				if (callback != CALLBACK_FAILED) u->u.rail.cached_vis_effect = callback;
ea737ba47ba0 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   206
			}
ea737ba47ba0 (svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents: 3900
diff changeset
   207
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
   208
			if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON &&
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   209
				UsesWagonOverride(u) && !HasBit(u->u.rail.cached_vis_effect, 7)) {
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
   210
				/* wagon is powered */
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
   211
				SetBit(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   212
			} else {
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
   213
				ClrBit(u->u.rail.flags, VRF_POWEREDWAGON);
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   214
			}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   215
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
   216
			/* 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
   217
			   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
   218
			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
   219
				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
   220
			}
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
   221
5116
3c0c3da214ca (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   222
			/* 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
   223
			 * 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
   224
			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
   225
				u->u.rail.railtype = RAILTYPE_RAIL;
3c0c3da214ca (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   226
				u->u.rail.compatible_railtypes |= (1 << RAILTYPE_RAIL);
3c0c3da214ca (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   227
			}
3c0c3da214ca (svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents: 4996
diff changeset
   228
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   229
			/* max speed is the minimum of the speed limits of all vehicles in the consist */
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
   230
			if ((rvi_u->railveh_type != RAILVEH_WAGON || _patches.wagon_speed_limits) && !UsesWagonOverride(u)) {
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
   231
				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
   232
				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
   233
			}
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   234
		}
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   235
6644
327f66bc01c6 (svn r9875) -Fix (r9828): Only set carriage capacity if the wagon has not been refitted.
peter1138
parents: 6643
diff changeset
   236
		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
   237
			/* 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
   238
			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
   239
		}
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
   240
7881
7838dbc8bbab (svn r11431) -Codechange: [NewGRF] Add support for changing rail vehicle user-defined bit mask (prop 25) via callback 36
glx
parents: 7833
diff changeset
   241
		u->u.rail.user_def_data = GetVehicleProperty(u, 0x25, rvi_u->user_def_data);
7838dbc8bbab (svn r11431) -Codechange: [NewGRF] Add support for changing rail vehicle user-defined bit mask (prop 25) via callback 36
glx
parents: 7833
diff changeset
   242
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   243
		/* check the vehicle length (callback) */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   244
		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
   245
		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
   246
			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
   247
		}
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
   248
		if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7881
diff changeset
   249
		veh_len = Clamp(veh_len, 0, u->Next() == NULL ? 7 : 5); // the clamp on vehicles not the last in chain is stricter, as too short wagons can break the 'follow next vehicle' code
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   250
		u->u.rail.cached_veh_length = 8 - veh_len;
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
   251
		v->u.rail.cached_total_length += u->u.rail.cached_veh_length;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   252
	}
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
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   254
	/* 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
   255
	v->u.rail.cached_max_speed = max_speed;
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
   256
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   257
	/* 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
   258
	TrainCargoChanged(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
   259
}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   260
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   261
/* These two arrays are used for realistic acceleration. XXX: How should they
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   262
 * be interpreted? */
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   263
static const byte _curve_neighbours45[8][2] = {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   264
	{7, 1},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   265
	{0, 2},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   266
	{1, 3},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   267
	{2, 4},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   268
	{3, 5},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   269
	{4, 6},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   270
	{5, 7},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   271
	{6, 0},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   272
};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   273
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   274
static const byte _curve_neighbours90[8][2] = {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   275
	{6, 2},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   276
	{7, 3},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   277
	{0, 4},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   278
	{1, 5},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   279
	{2, 6},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   280
	{3, 7},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   281
	{4, 0},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   282
	{5, 1},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   283
};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   284
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   285
enum AccelType {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   286
	AM_ACCEL,
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   287
	AM_BRAKE
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   288
};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   289
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   290
static bool TrainShouldStop(const Vehicle* v, TileIndex tile)
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   291
{
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   292
	const Order* o = &v->current_order;
3315
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   293
	StationID sid = GetStationIndex(tile);
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   294
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
   295
	assert(v->type == VEH_TRAIN);
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   296
	/* When does a train drive through a station
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   297
	 * first we deal with the "new nonstop handling" */
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   298
	if (_patches.new_nonstop && o->flags & OF_NON_STOP && sid == o->dest) {
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   299
		return false;
3315
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   300
	}
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   301
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   302
	if (v->last_station_visited == sid) return false;
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   303
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   304
	if (sid != o->dest && (o->flags & OF_NON_STOP || _patches.new_nonstop)) {
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   305
		return false;
3315
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3269
diff changeset
   306
	}
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   307
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   308
	return true;
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   309
}
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   310
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   311
/** new acceleration*/
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   312
static int GetTrainAcceleration(Vehicle *v, bool mode)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   313
{
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   314
	int max_speed = 2000;
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
   315
	int speed = v->cur_speed * 10 / 16; // km-ish/h -> mp/h
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   316
	int curvecount[2] = {0, 0};
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   317
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   318
	/*first find the curve speed limit */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   319
	int numcurve = 0;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   320
	int sum = 0;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   321
	int pos = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   322
	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
   323
	for (const Vehicle *u = v; u->Next() != NULL; u = u->Next(), pos++) {
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
   324
		Direction dir = u->direction;
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
   325
		Direction ndir = u->Next()->direction;
1531
4ddd9437139b (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   326
		int i;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   327
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   328
		for (i = 0; i < 2; i++) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   329
			if ( _curve_neighbours45[dir][i] == ndir) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   330
				curvecount[i]++;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   331
				if (lastpos != -1) {
1531
4ddd9437139b (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   332
					numcurve++;
4ddd9437139b (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   333
					sum += pos - lastpos;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   334
					if (pos - lastpos == 1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   335
						max_speed = 88;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   336
					}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   337
				}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   338
				lastpos = pos;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   339
			}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   340
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   341
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   342
		/*if we have a 90 degree turn, fix the speed limit to 60 */
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   343
		if (_curve_neighbours90[dir][0] == ndir ||
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   344
				_curve_neighbours90[dir][1] == ndir) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   345
			max_speed = 61;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   346
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   347
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   348
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   349
	if (numcurve > 0) sum /= numcurve;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   350
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   351
	if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   352
		int total = curvecount[0] + curvecount[1];
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   353
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   354
		if (curvecount[0] == 1 && curvecount[1] == 1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   355
			max_speed = 0xFFFF;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   356
		} else if (total > 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
   357
			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
   358
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   359
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   360
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   361
	max_speed += (max_speed / 2) * v->u.rail.railtype;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   362
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   363
	if (IsTileType(v->tile, MP_STATION) && IsFrontEngine(v)) {
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   364
		if (TrainShouldStop(v, 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
   365
			int station_length = GetStationByTile(v->tile)->GetPlatformLength(v->tile, DirToDiagDir(v->direction));
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   366
			int delta_v;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   367
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   368
			max_speed = 120;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   369
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   370
			delta_v = v->cur_speed / (station_length + 1);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   371
			if (v->max_speed > (v->cur_speed - delta_v))
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   372
				max_speed = v->cur_speed - (delta_v / 10);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   373
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   374
			max_speed = max(max_speed, 25 * station_length);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   375
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   376
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   377
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   378
	int mass = v->u.rail.cached_weight;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   379
	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
   380
	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
   381
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   382
	int num = 0; //number of vehicles, change this into the number of axles later
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   383
	int incl = 0;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   384
	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
   385
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   386
		num++;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   387
		drag_coeff += 3;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   388
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   389
		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
   390
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   391
		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
   392
			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
   393
		} 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
   394
			incl -= u->u.rail.cached_veh_weight * 60;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   395
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   396
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   397
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   398
	v->max_speed = max_speed;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   399
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   400
	const int area = 120;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   401
	const int friction = 35; //[1e-3]
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   402
	int resistance;
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   403
	if (v->u.rail.railtype != RAILTYPE_MAGLEV) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   404
		resistance = 13 * mass / 10;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   405
		resistance += 60 * num;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   406
		resistance += friction * mass * speed / 1000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   407
		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
   408
	} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   409
		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
   410
	}
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   411
	resistance += incl;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   412
	resistance *= 4; //[N]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   413
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
   414
	/* 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
   415
	 * actually double the train's power */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   416
	const int max_te = v->u.rail.cached_max_te; // [N]
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   417
	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
   418
	if (speed > 2) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   419
		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
   420
			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
   421
			case RAILTYPE_ELECTRIC:
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   422
			case RAILTYPE_MONO:
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   423
				force = power / speed; //[N]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   424
				force *= 22;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   425
				force /= 10;
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   426
				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
   427
				break;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   428
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   429
			default: NOT_REACHED();
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   430
			case RAILTYPE_MAGLEV:
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   431
				force = power / 25;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   432
				break;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   433
		}
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   434
	} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   435
		/* "kickoff" acceleration */
5400
bba42ad3223b (svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents: 5385
diff changeset
   436
		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
   437
		force = max(force, (mass * 8) + resistance);
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   438
	}
1179
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
	if (force <= 0) force = 10000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   441
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   442
	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
   443
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   444
	if (mode == AM_ACCEL) {
1684
3b2df64cc706 (svn r2188) Revert r2185, because trains always accerlated (Requested by Celestar)
tron
parents: 1683
diff changeset
   445
		return (force - resistance) / (mass * 4);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   446
	} else {
5279
295d8b87b394 (svn r7425) -Fix (r1681): With realistic acceleration, guarantee a minimum braking
peter1138
parents: 5277
diff changeset
   447
		return min((-force - resistance) / (mass * 4), -10000 / (mass * 4));
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   448
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   449
}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   450
2817
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2805
diff changeset
   451
static void UpdateTrainAcceleration(Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
{
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   453
	assert(IsFrontEngine(v));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
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
   455
	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
   456
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   457
	uint power = v->u.rail.cached_power;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   458
	uint weight = v->u.rail.cached_weight;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
	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
   460
	v->acceleration = Clamp(power / weight * 4, 1, 255);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
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
   463
int Train::GetImage(Direction direction) const
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
{
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
   465
	int img = this->spritenum;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
	int base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   468
	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
   469
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
	if (is_custom_sprite(img)) {
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
   471
		base = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   472
		if (base != 0) return base;
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
   473
		img = orig_rail_vehicle_info[this->engine_type].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   475
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
	base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
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
   478
	if (this->cargo.Count() >= this->cargo_cap / 2U) base += _wagon_full_adder[img];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
	return base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   481
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   482
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
   483
{
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   484
	const RailVehicleInfo *rvi = RailVehInfo(engine);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   485
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
	int img = rvi->image_index;
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   487
	SpriteID image = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   488
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
	if (is_custom_sprite(img)) {
3186
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
   490
		image = GetCustomVehicleIcon(engine, DIR_W);
2570
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   491
		if (image == 0) {
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   492
			img = orig_rail_vehicle_info[engine].image_index;
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   493
		} else {
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   494
			y += _traininfo_vehicle_pitch;
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   495
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
	}
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   497
	if (image == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
		image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
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
   501
	if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   502
		DrawSprite(image, pal, x - 14, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
		x += 15;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   504
		image = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
		if (is_custom_sprite(img)) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   506
			image = GetCustomVehicleIcon(engine, DIR_E);
2464
e3054f0f0627 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2463
diff changeset
   507
			if (image == 0) img = orig_rail_vehicle_info[engine].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
		}
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   509
		if (image == 0) {
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   510
			image =
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   511
				((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) +
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   512
				_engine_sprite_base[img + 1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
	}
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   515
	DrawSprite(image, pal, x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   518
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
   519
{
889
64d2f7d5f02b (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 882
diff changeset
   520
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
64d2f7d5f02b (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 882
diff changeset
   521
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   522
	const RailVehicleInfo *rvi = RailVehInfo(engine);
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   523
	CommandCost value((GetEngineProperty(engine, 0x17, rvi->base_cost) * _price.build_railwagon) >> 8);
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   524
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
   525
	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
   526
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
	if (!(flags & DC_QUERY_COST)) {
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
   528
		/* Allow for the wagon and the articulated parts, plus one to "terminate" the list. */
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
   529
		Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
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
   530
		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
   531
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
   532
		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
   533
			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
   534
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
		if (flags & DC_EXEC) {
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   536
			Vehicle *v = vl[0];
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   537
			v->spritenum = rvi->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   539
			Vehicle *u = NULL;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   540
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   541
			Vehicle *w;
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   542
			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
   543
				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
   544
				    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
   545
				    !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
   546
					u = GetLastVehicleInChain(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
7782
e2999b5fcfad (svn r11332) -Fix: vehicles getting a value of 0 on construction.
rubidium
parents: 7696
diff changeset
   551
			v = new (v) Train();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
			v->engine_type = engine;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   554
			DiagDirection dir = GetRailDepotDirection(tile);
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   555
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   556
			v->direction = DiagDirToDir(dir);
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   557
			v->tile = tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   558
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   559
			int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   560
			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
   561
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
			v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
			v->y_pos = y;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   564
			v->z_pos = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
			v->owner = _current_player;
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   566
			v->u.rail.track = TRACK_BIT_DEPOT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
			v->vehstatus = VS_HIDDEN | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   569
			v->subtype = 0;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   570
			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
   571
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
			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
   573
				u->SetNext(v);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   574
			} else {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   575
				SetFreeWagon(v);
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
   576
				InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
			v->cargo_type = rvi->cargo_type;
3870
d9ebc4ea750a (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3836
diff changeset
   580
			v->cargo_subtype = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
			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
   582
			v->value = value.GetCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
//			v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
   585
			v->u.rail.railtype = rvi->railtype;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   586
4329
0e6e689f66e7 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   587
			v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
			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
   589
			v->random_bits = VehicleRandomBits();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   590
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
   591
			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
   592
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6789
diff changeset
   593
			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
   594
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   595
			_new_vehicle_id = v->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
			VehiclePositionChanged(v);
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   598
			TrainConsistChanged(v->First());
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   599
			UpdateTrainGroupID(v->First());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
2970
3b9380f10074 (svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents: 2962
diff changeset
   602
			if (IsLocalPlayer()) {
7425
d9bc116f2f54 (svn r10823) -Fix r8610 [FS#1097]: the autoreplace window vehicle count didn't always update correctly) (Matthias)
bjarni
parents: 7398
diff changeset
   603
				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
   604
			}
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5922
diff changeset
   605
			GetPlayer(_current_player)->num_engines[engine]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
		}
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
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   609
	return CommandCost(value);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   612
/** Move all free vehicles in the depot to the train */
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   613
static void NormalizeTrainVehInDepot(const Vehicle* u)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
{
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   615
	const Vehicle* v;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   616
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
	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
   618
		if (v->type == VEH_TRAIN && IsFreeWagon(v) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
				v->tile == u->tile &&
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   620
				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
   621
			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
   622
					CMD_MOVE_RAIL_VEHICLE)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   628
static CommandCost EstimateTrainCost(EngineID engine, const RailVehicleInfo* rvi)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
{
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   630
	return CommandCost(GetEngineProperty(engine, 0x17, rvi->base_cost) * (_price.build_railvehicle >> 3) >> 5);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
2817
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2805
diff changeset
   633
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
   634
{
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   635
	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
   636
	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
   637
	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
   638
	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
   639
	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
   640
	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
   641
	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
   642
	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
   643
	u->vehstatus = v->vehstatus & ~VS_STOPPED;
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   644
	u->subtype = 0;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   645
	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
   646
	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
   647
	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
   648
	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
   649
	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
   650
	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
   651
	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
   652
	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
   653
	u->build_year = v->build_year;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   654
	if (building) v->value >>= 1;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   655
	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
   656
	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
   657
	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
   658
	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
   659
}
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   660
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   661
/** 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
   662
 * @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
   663
 * @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
   664
 * @param p1 engine type id
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
   665
 * @param p2 bit 0 when set, the train will get number 0, otherwise it will get a free number
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
   666
 *           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
   667
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   668
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
   669
{
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   670
	/* Check if the engine-type is valid (for the player) */
6407
757a3cec2422 (svn r9543) -Fix [FS#691]: Split up STR_ENGINE_NOT_BUILDABLE into four strings so it can be
maedhros
parents: 6357
diff changeset
   671
	if (!IsEngineBuildable(p1, VEH_TRAIN, _current_player)) 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
   672
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   673
	/* Check if the train is actually being built in a depot belonging
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   674
	 * to the player. 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
   675
	if (!(flags & DC_QUERY_COST)) {
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
   676
		if (!IsTileDepotType(tile, TRANSPORT_RAIL)) return CMD_ERROR;
1901
03bf9bf99319 (svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents: 1895
diff changeset
   677
		if (!IsTileOwner(tile, _current_player)) 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
   678
	}
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
   679
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   680
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   682
	const RailVehicleInfo *rvi = RailVehInfo(p1);
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   683
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   684
	/* Check if depot and new engine uses the same kind of tracks */
3411
72993ac027ea (svn r4226) -Fix: [elrails] it's no longer possible to build electric engines in non-electric depots
bjarni
parents: 3390
diff changeset
   685
	/* We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
   686
	if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   687
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
   688
	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
   689
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   690
	CommandCost value = EstimateTrainCost(p1, rvi);
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   691
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   692
	uint num_vehicles =
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   693
		(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
   694
		CountArticulatedParts(p1, false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
	if (!(flags & DC_QUERY_COST)) {
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
   697
		/* Allow for the dual-heads and the articulated parts, plus one to "terminate" the list. */
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
   698
		Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
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
   699
		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
   700
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
   701
		if (!Vehicle::AllocateList(vl, num_vehicles))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
			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
   703
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   704
		Vehicle *v = vl[0];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   705
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   706
		UnitID unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_TRAIN);
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
   707
		if (unit_num > _patches.max_trains)
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
   708
			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
   709
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
		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
   711
			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
   712
			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
   713
			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
   714
7783
b6c199abdd23 (svn r11333) -Fix: Train engines still had a value of 0.
maedhros
parents: 7782
diff changeset
   715
			v = new (v) Train();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
			v->unitnumber = unit_num;
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   717
			v->direction = DiagDirToDir(dir);
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   718
			v->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
			v->owner = _current_player;
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
   720
			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
   721
			v->y_pos = y;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   722
			v->z_pos = GetSlopeZ(x, y);
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
   723
			v->u.rail.track = TRACK_BIT_DEPOT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
			v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
			v->spritenum = rvi->image_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
			v->cargo_type = rvi->cargo_type;
3870
d9ebc4ea750a (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3836
diff changeset
   727
			v->cargo_subtype = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
			v->cargo_cap = rvi->capacity;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   729
			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
   730
			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
   731
			v->last_station_visited = INVALID_STATION;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
			v->dest_tile = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   733
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
   734
			v->engine_type = p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
   736
			const Engine *e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
			v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
			v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
			v->max_age = e->lifelength * 366;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   740
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
			v->string_id = STR_SV_TRAIN_NAME;
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
   742
			v->u.rail.railtype = rvi->railtype;
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   743
			_new_vehicle_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   744
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
			v->service_interval = _patches.servint_trains;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
			v->date_of_last_service = _date;
4329
0e6e689f66e7 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   747
			v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   748
			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
   749
			v->random_bits = VehicleRandomBits();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   750
6176
153f0f24dc1b (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6175
diff changeset
   751
			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
   752
			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
   753
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
   754
			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
   755
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   756
			v->subtype = 0;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   757
			SetFrontEngine(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   758
			SetTrainEngine(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   759
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   760
			VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   761
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
   762
			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
   763
				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
   764
				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
   765
				/* 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
   766
				 * 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
   767
				 * 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
   768
				 */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   769
				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
   770
				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
   771
			} else {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6789
diff changeset
   772
				AddArticulatedParts(vl, VEH_TRAIN);
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   773
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   774
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
   775
			TrainConsistChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   776
			UpdateTrainAcceleration(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
   777
			UpdateTrainGroupID(v);
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   778
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   779
			if (!HasBit(p2, 1)) { // check if the cars should be added to the new vehicle
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   780
				NormalizeTrainVehInDepot(v);
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   781
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   782
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
   783
			InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   784
			RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
			InvalidateWindow(WC_COMPANY, v->owner);
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5922
diff changeset
   786
			if (IsLocalPlayer())
7425
d9bc116f2f54 (svn r10823) -Fix r8610 [FS#1097]: the autoreplace window vehicle count didn't always update correctly) (Matthias)
bjarni
parents: 7398
diff changeset
   787
				InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5922
diff changeset
   788
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5922
diff changeset
   789
			GetPlayer(_current_player)->num_engines[p1]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   790
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
	}
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
   792
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   793
	return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   796
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   797
/* 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
   798
 * 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
   799
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
   800
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
	TileIndex tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   802
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
	/* check if stopped in a 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
   804
	if (!IsTileDepotType(tile, TRANSPORT_RAIL) || v->cur_speed != 0) return -1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   806
	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
   807
	for (; v != NULL; v = v->Next()) {
2844
3c0e05cc5f1a (svn r3392) Train depot fixes:
peter1138
parents: 2840
diff changeset
   808
		/* 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
   809
		 * 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
   810
		 * 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
   811
		 *
5dc801277eea (svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents: 3977
diff changeset
   812
		 * 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
   813
		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
   814
		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
   815
				(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
   816
			return -1;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   817
		}
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   818
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
	return count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
4529
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   823
/* 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
   824
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
   825
{
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   826
	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
   827
}
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   828
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   829
/* 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
   830
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
   831
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   832
	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
   833
}
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   834
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   835
/**
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   836
 * 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
   837
 * @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
   838
 * @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
   839
 * @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
   840
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   843
	/* unlinking the first vehicle of the chain? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   844
	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
   845
		v = GetNextVehicle(v);
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   846
		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
   847
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   848
		if (IsTrainWagon(v)) SetFreeWagon(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   849
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
		return v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
	}
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
   852
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   853
	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
   854
	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
   855
	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
   856
	return first;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   857
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   858
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   859
static Vehicle *FindGoodVehiclePos(const Vehicle *src)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
	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
   862
	EngineID eng = src->engine_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   863
	TileIndex tile = src->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
	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
   866
		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
   867
			/* 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
   868
			Vehicle *v = dst;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   869
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
			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
   871
				v = v->Next();
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   872
				if (v == NULL) return dst;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   874
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   878
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   879
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   880
/*
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   881
 * 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
   882
 * 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
   883
 */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   884
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
   885
{
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   886
	UnlinkWagon(v, v->First());
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   887
	if (dest == NULL) return;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   888
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   889
	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
   890
	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
   891
	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
   892
	v->SetNext(next);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   893
	ClearFreeWagon(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   894
	ClearFrontEngine(v);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   895
}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   896
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   897
/*
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   898
 * 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
   899
 * 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
   900
 */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   901
static void NormaliseTrainConsist(Vehicle *v)
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   902
{
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   903
	if (IsFreeWagon(v)) return;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   904
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   905
	assert(IsFrontEngine(v));
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   906
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   907
	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
   908
		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
   909
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   910
		/* make sure that there are no free cars before next engine */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   911
		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
   912
		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
   913
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   914
		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
   915
		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
   916
	}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   917
}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   918
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   919
/** 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
   920
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   921
 * @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
   922
 * @param p1 various bitstuffed elements
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   923
 * - p1 (bit  0 - 15) source vehicle index
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   924
 * - 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
   925
 * @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
   926
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   927
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
   928
{
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
   929
	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
   930
	VehicleID d = GB(p1, 16, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   932
	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
   933
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   934
	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
   935
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
   936
	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
   937
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
   938
	/* 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
   939
	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
   940
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   941
	/* 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
   942
	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
   943
	if (d == INVALID_VEHICLE) {
4198
f8d95f4d9af1 (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
   944
		dst = IsTrainEngine(src) ? NULL : FindGoodVehiclePos(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
	} else {
6136
6a4ee6549aaf (svn r8878) -Fix
tron
parents: 6114
diff changeset
   946
		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
   947
		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
   948
		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
   949
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
   950
		/* 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
   951
		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
   952
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   954
	/* 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
   955
	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
   956
	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
   957
		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
   958
	}
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   959
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   960
	/* 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
   961
	if (src == dst) return CommandCost();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   962
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
	/* 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
   964
	Vehicle *src_head = src->First();
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   965
	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
   966
	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
   967
		dst_head = dst->First();
6136
6a4ee6549aaf (svn r8878) -Fix
tron
parents: 6114
diff changeset
   968
		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
   969
		/* 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
   970
		dst = GetLastEnginePart(dst);
6136
6a4ee6549aaf (svn r8878) -Fix
tron
parents: 6114
diff changeset
   971
	} else {
6a4ee6549aaf (svn r8878) -Fix
tron
parents: 6114
diff changeset
   972
		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
   973
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   974
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
   975
	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
   976
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   977
	/* 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
   978
	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
   979
2883
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
   980
	{
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
   981
		int max_len = _patches.mammoth_trains ? 100 : 9;
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
   982
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   983
		/* check if all vehicles in the source train are stopped inside a depot. */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
   984
		int src_len = CheckTrainStoppedInDepot(src_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
   985
		if (src_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
   986
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   987
		/* 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
   988
		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
   989
			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
   990
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
   991
			if (dst_head != NULL) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   992
				/* 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
   993
				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
   994
				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
   995
			}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
   996
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
   997
			/* 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
   998
			 * 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
   999
			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
  1000
				const Vehicle *u;
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1001
				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
  1002
					src_len--;
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1003
			} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1004
				/* 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
  1005
				src_len = 1;
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1006
			}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1007
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1008
			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
  1009
				/* 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
  1010
				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
  1011
				/* 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
  1012
				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
  1013
			}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1014
		} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1015
			/* 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
  1016
			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
  1017
				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
  1018
		}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1019
	}
90f7db6e078e (svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents: 2874
diff changeset
  1020
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1021
	/* 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
  1022
	if (dst == NULL && !IsFrontEngine(src) && IsTrainEngine(src)) {
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
  1023
		UnitID unit_num = GetFreeUnitNumber(VEH_TRAIN);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
		if (unit_num > _patches.max_trains)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1025
			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
  1026
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
  1027
		if (flags & DC_EXEC) src->unitnumber = unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1029
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
  1030
	if (dst_head != NULL) {
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
  1031
		/* Check NewGRF Callback 0x1D */
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
  1032
		uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, dst_head->engine_type, src, dst_head);
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
  1033
		if (callback != CALLBACK_FAILED) {
3729
db4040124d25 (svn r4706) - NewGRF: add the correct default error message for the wagon attach callback.
peter1138
parents: 3727
diff changeset
  1034
			if (callback == 0xFD) return_cmd_error(STR_INCOMPATIBLE_RAIL_TYPES);
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
  1035
			if (callback < 0xFD) {
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
  1036
				StringID error = GetGRFStringID(GetEngineGRFID(dst_head->engine_type), 0xD000 + callback);
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
  1037
				return_cmd_error(error);
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
  1038
			}
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
  1039
		}
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
  1040
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1042
	/* do it? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
	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
  1044
		/* 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
  1045
		   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
  1046
		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
  1047
			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
  1048
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
  1049
			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
  1050
				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
  1051
				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
  1052
			}
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
  1053
		}
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
  1054
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1055
		if (HasBit(p2, 0)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1056
			/* unlink ALL wagons */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1057
			if (src != src_head) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
				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
  1059
				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
  1060
				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
  1061
			} else {
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1062
				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
  1063
				src_head = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
		} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1066
			/* 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
  1067
			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
  1068
			/* 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
  1069
			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
  1070
			GetLastEnginePart(src)->SetNext(NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1071
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
		if (dst == NULL) {
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1074
			/* 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
  1075
			InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1076
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1077
			/* 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
  1078
			if (IsTrainEngine(src)) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1079
				if (!IsFrontEngine(src)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1080
					/* 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
  1081
					SetFrontEngine(src);
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1082
					assert(src->orders == NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
					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
  1084
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
  1085
					// Decrease the engines number of the 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
  1086
					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
  1087
						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
  1088
					}
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
  1089
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
  1090
					// If we move an engine to a new line affect it 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
  1091
					src->group_id = DEFAULT_GROUP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1092
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1093
			} else {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1094
				SetFreeWagon(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1095
			}
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
  1096
			dst_head = src;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1097
		} else {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1098
			if (IsFrontEngine(src)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1099
				/* 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
  1100
				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
  1101
				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
  1102
				RemoveVehicleFromGroup(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1104
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1105
			if (IsFrontEngine(src) || IsFreeWagon(src)) {
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1106
				InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1107
				ClearFrontEngine(src);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1108
				ClearFreeWagon(src);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1109
				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
  1110
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1112
			/* link in the wagon(s) in the chain. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1113
			{
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1114
				Vehicle *v;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1115
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1116
				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
  1117
				GetLastEnginePart(v)->SetNext(dst->Next());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
			}
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
  1119
			dst->SetNext(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
		}
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1121
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1122
		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
  1123
			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
  1124
				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
  1125
			}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1126
			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
  1127
		}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1128
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
  1129
		/* 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
  1130
		 * 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
  1131
		 * 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
  1132
		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
  1133
			/* 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
  1134
			 * 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
  1135
			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
  1136
			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
  1137
			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
  1138
			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
  1139
		}
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
  1140
4198
f8d95f4d9af1 (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1141
		if (src_head != NULL) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1142
			NormaliseTrainConsist(src_head);
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
  1143
			TrainConsistChanged(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
  1144
			UpdateTrainGroupID(src_head);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1145
			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
  1146
				UpdateTrainAcceleration(src_head);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1147
				InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1148
				/* 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
  1149
				InvalidateWindow(WC_VEHICLE_REFIT, src_head->index);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1150
				InvalidateWindowWidget(WC_VEHICLE_VIEW, src_head->index, 12);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1151
			}
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1152
			/* 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
  1153
			InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile);
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1154
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
4198
f8d95f4d9af1 (svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents: 4197
diff changeset
  1156
		if (dst_head != NULL) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1157
			NormaliseTrainConsist(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
  1158
			TrainConsistChanged(dst_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
  1159
			UpdateTrainGroupID(dst_head);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1160
			if (IsFrontEngine(dst_head)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1161
				UpdateTrainAcceleration(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
  1162
				InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1163
				/* 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
  1164
				InvalidateWindowWidget(WC_VEHICLE_VIEW, dst_head->index, 12);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1165
				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
  1166
			}
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
  1167
			/* 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
  1168
			InvalidateWindow(WC_VEHICLE_DEPOT, dst_head->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1169
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1170
737
05082439a150 (svn r1189) Fix vehicle list update glitch when moving waggons in depots
tron
parents: 715
diff changeset
  1171
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1172
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1173
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1174
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1176
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1177
/** Start/Stop a 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
  1178
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1179
 * @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
  1180
 * @param p1 train to start/stop
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1181
 * @param p2 unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1182
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1183
CommandCost CmdStartStopTrain(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
{
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1185
	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
  1186
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1187
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1188
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
  1189
	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
  1190
4244
d40c73c55357 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1191
	/* Check if this train can be started/stopped. The callback will fail or
d40c73c55357 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1192
	 * return 0xFF if it can. */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1193
	uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
4244
d40c73c55357 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1194
	if (callback != CALLBACK_FAILED && callback != 0xFF) {
d40c73c55357 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1195
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
d40c73c55357 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1196
		return_cmd_error(error);
d40c73c55357 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1197
	}
d40c73c55357 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
  1198
4251
e8f69de723e1 (svn r5853) -Fix: [elrails] FS#178 Electric Trains can leave Conventional Depot
bjarni
parents: 4244
diff changeset
  1199
	if (v->vehstatus & VS_STOPPED && v->u.rail.cached_power == 0) return_cmd_error(STR_TRAIN_START_NO_CATENARY);
e8f69de723e1 (svn r5853) -Fix: [elrails] FS#178 Electric Trains can leave Conventional Depot
bjarni
parents: 4244
diff changeset
  1200
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1201
	if (flags & DC_EXEC) {
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1202
		if (v->vehstatus & VS_STOPPED && v->u.rail.track == TRACK_BIT_DEPOT) {
3139
b17abef09e15 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3022
diff changeset
  1203
			DeleteVehicleNews(p1, STR_8814_TRAIN_IS_WAITING_IN_DEPOT);
b17abef09e15 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3022
diff changeset
  1204
		}
b17abef09e15 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3022
diff changeset
  1205
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1206
		v->vehstatus ^= VS_STOPPED;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  1207
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1208
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
	}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1210
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1211
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1212
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1213
/** 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
  1214
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1215
 * @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
  1216
 * @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
  1217
 * @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
  1218
 * - 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
  1219
 * - p2 = 1: sell the vehicle and all vehicles following it in the chain
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1220
             if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1221
 * - 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
  1222
 *           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
  1223
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1224
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
  1225
{
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
  1226
	/* 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
  1227
	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
  1228
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1229
	if (!IsValidVehicleID(p1) || p2 > 2) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1230
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1231
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1232
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
  1233
	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
  1234
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
  1235
	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
  1236
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
  1237
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1238
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1239
	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
  1240
	Vehicle *first = v->First();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1241
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1242
	/* 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
  1243
	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
  1244
		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
  1245
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1246
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
  1247
	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
  1248
1842
fb9086b04740 (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1249
	if (flags & DC_EXEC) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1250
		if (v == first && IsFrontEngine(first)) {
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
  1251
			w = FindWindowById(WC_VEHICLE_VIEW, first->index);
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
  1252
			if (w != NULL) DeleteWindow(w);
2618
9691753519ac (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
  1253
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
		InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
1779
25b3028dc97c (svn r2283) - Fix (regression): [ 1197493 ] train_gui.c:1341: failed assertion ..., somehow RebuildVehicleList() got lost during the rewrite :P
Darkvater
parents: 1777
diff changeset
  1255
		RebuildVehicleLists();
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
  1256
	}
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
  1257
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1258
	CommandCost cost;
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
  1259
	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
  1260
		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
  1261
			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
  1262
			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
  1263
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
  1264
			/* 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
  1265
			 * 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
  1266
			Vehicle *rear = (IsMultiheaded(v) &&
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1267
				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
  1268
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
  1269
			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
  1270
				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
  1271
				if (flags & DC_EXEC) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1272
					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
  1273
					DeleteDepotHighlightOfVehicle(rear);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  1274
					delete rear;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1275
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
			}
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
  1277
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
  1278
			/* 2. We are selling the first engine, 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
  1279
			 * here, so take attention */
1770
18491bba9211 (svn r2274) - Codechange: some comments, parentheses and EngineID typedef for engine_type
Darkvater
parents: 1766
diff changeset
  1280
			if ((flags & DC_EXEC) && v == first) {
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1281
				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
  1282
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
  1283
				/* 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
  1284
				 * 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
  1285
				 * 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
  1286
				if (new_f != NULL && IsTrainEngine(new_f)) {
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
  1287
					switch_engine = true;
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
  1288
					/* Copy important data from the front engine */
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
  1289
					new_f->unitnumber      = first->unitnumber;
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
  1290
					new_f->current_order   = first->current_order;
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
  1291
					new_f->cur_order_index = first->cur_order_index;
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
  1292
					new_f->orders          = first->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
  1293
					new_f->num_orders      = first->num_orders;
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
  1294
					new_f->group_id        = first->group_id;
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
  1295
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
  1296
					if (first->prev_shared != NULL) {
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
  1297
						first->prev_shared->next_shared = new_f;
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
  1298
						new_f->prev_shared = first->prev_shared;
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
  1299
					}
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
  1300
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
  1301
					if (first->next_shared != NULL) {
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
  1302
						first->next_shared->prev_shared = new_f;
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
  1303
						new_f->next_shared = first->next_shared;
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
  1304
					}
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
  1305
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
  1306
					/*
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
  1307
					 * Remove all order information from the front train, to
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
  1308
					 * prevent the order and the shared order list to be
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
  1309
					 * destroyed by Destroy/DeleteVehicle.
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
  1310
					 */
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
  1311
					first->orders      = NULL;
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
  1312
					first->prev_shared = NULL;
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
  1313
					first->next_shared = NULL;
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
  1314
					first->group_id    = DEFAULT_GROUP;
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
  1315
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
  1316
					/* If we deleted a window then open a new one for the 'new' train */
7486
bf6a94009823 (svn r10997) -Codechange: unify the vehicle view window. Patch by PhilSophus.
rubidium
parents: 7477
diff changeset
  1317
					if (IsLocalPlayer() && w != NULL) ShowVehicleViewWindow(new_f);
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
  1318
				}
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
  1319
			}
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
  1320
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
  1321
			/* 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
  1322
			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
  1323
			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
  1324
				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
  1325
				DeleteDepotHighlightOfVehicle(v);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  1326
				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
  1327
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
  1328
				/* 4 If the second wagon was an engine, update it to front_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
  1329
					* 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
  1330
				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
  1331
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
  1332
				/* 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
  1333
				if (first != NULL) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1334
					NormaliseTrainConsist(first);
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
  1335
					TrainConsistChanged(first);
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
  1336
					UpdateTrainGroupID(first);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1337
					if (IsFrontEngine(first)) {
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
  1338
						InvalidateWindow(WC_VEHICLE_DETAILS, first->index);
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
						InvalidateWindow(WC_VEHICLE_REFIT, first->index);
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
						UpdateTrainAcceleration(first);
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
					}
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
  1342
				}
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
  1343
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
  1344
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
  1345
				/* (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
  1346
				 * 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
  1347
				 * 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
  1348
				 * engines to its train anyways */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1349
				if (p2 == 2 && HasBit(ori_subtype, Train_Front)) {
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1350
					Vehicle *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
  1351
					for (v = first; 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
  1352
						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
  1353
						DoCommand(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
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
  1354
					}
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
  1355
				}
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
  1356
			}
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
  1357
		} 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
  1358
		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
  1359
			/* 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
  1360
			 * 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
  1361
			 * earlier in the chain (before deletion), leave it alone */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1362
			Vehicle *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
  1363
			for (; 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
  1364
				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
  1365
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1366
				if (IsMultiheaded(v)) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1367
					if (IsTrainEngine(v)) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1368
						/* 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
  1369
						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
  1370
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1371
						if (rear != NULL) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1372
							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
  1373
b0ba7d1b9096 (svn r10023) -Fix (r3218): When selling trains, if there were no wagons between multiheaded
maedhros
parents: 6784
diff changeset
  1374
							/* 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
  1375
							 * 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
  1376
							 * 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
  1377
							 * 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
  1378
							 * 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
  1379
							 * 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
  1380
							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
  1381
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1382
							if (flags & DC_EXEC) {
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1383
								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
  1384
								DeleteDepotHighlightOfVehicle(rear);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  1385
								delete rear;
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1386
							}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1387
						}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1388
					} 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
  1389
						/* 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
  1390
						continue;
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1391
					}
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
  1392
				}
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
  1393
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1394
				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
  1395
				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
  1396
					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
  1397
					DeleteDepotHighlightOfVehicle(v);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  1398
					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
  1399
				}
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
  1400
			}
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
  1401
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
  1402
			/* 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
  1403
			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
  1404
				NormaliseTrainConsist(first);
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
  1405
				TrainConsistChanged(first);
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
  1406
				UpdateTrainGroupID(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
  1407
				if (IsFrontEngine(first)) UpdateTrainAcceleration(first);
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1408
				InvalidateWindow(WC_VEHICLE_DETAILS, first->index);
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1409
				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
  1410
			}
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
  1411
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1412
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1413
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1414
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1415
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1416
void Train::UpdateDeltaXY(Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1417
{
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1418
#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
  1419
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1420
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1421
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1422
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1423
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1424
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1425
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1426
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1427
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1428
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1429
#undef MKIT
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1430
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1431
	uint32 x = _delta_xy_table[direction];
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1432
	this->x_offs        = GB(x,  0, 8);
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1433
	this->y_offs        = GB(x,  8, 8);
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1434
	this->sprite_width  = GB(x, 16, 8);
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1435
	this->sprite_height = GB(x, 24, 8);
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1436
	this->z_height      = 6;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1437
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1438
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1439
static void UpdateVarsAfterSwap(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1440
{
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  1441
	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
  1442
	v->cur_image = v->GetImage(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1443
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1444
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1445
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1446
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1447
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1448
static void SetLastSpeed(Vehicle* v, int spd)
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1449
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1450
	int old = v->u.rail.last_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1451
	if (spd != old) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1452
		v->u.rail.last_speed = spd;
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
  1453
		if (_patches.vehicle_speed || (old == 0) != (spd == 0))
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  1454
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1455
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1456
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1457
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1458
static void SwapTrainFlags(byte *swap_flag1, byte *swap_flag2)
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1459
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1460
	byte flag1 = *swap_flag1;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1461
	byte 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
  1462
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1463
	/* 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
  1464
	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
  1465
	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
  1466
	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
  1467
	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
  1468
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1469
	/* 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
  1470
	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
  1471
		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
  1472
	} 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
  1473
		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
  1474
	}
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1475
	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
  1476
		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
  1477
	} 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
  1478
		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
  1479
	}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1480
}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1481
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1482
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1483
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1484
	Vehicle *a, *b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1485
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1486
	/* 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
  1487
	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
  1488
	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
  1489
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1490
	if (a != b) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1491
		/* swap the hidden bits */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1492
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1493
			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
  1494
			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
  1495
			a->vehstatus = tmp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1496
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1497
5733
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1498
		Swap(a->u.rail.track, b->u.rail.track);
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1499
		Swap(a->direction,    b->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1500
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1501
		/* toggle direction */
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1502
		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
  1503
		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
  1504
5733
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1505
		Swap(a->x_pos, b->x_pos);
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1506
		Swap(a->y_pos, b->y_pos);
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1507
		Swap(a->tile,  b->tile);
388bb9dcb79b (svn r8276) -Fix
tron
parents: 5668
diff changeset
  1508
		Swap(a->z_pos, b->z_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1509
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1510
		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
  1511
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1512
		/* update other vars */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
		UpdateVarsAfterSwap(a);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1514
		UpdateVarsAfterSwap(b);
1554
79809a004e97 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1515
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  1516
		/* 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
  1517
		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
  1518
		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
  1519
	} else {
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1520
		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
  1521
		UpdateVarsAfterSwap(a);
1554
79809a004e97 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1522
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  1523
		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
  1524
	}
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
  1525
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
  1526
	/* 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
  1527
	TrainPowerChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
744
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1530
/* Check if the vehicle is a train and is on the tile we are testing */
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1531
static void *TestTrainOnCrossing(Vehicle *v, void *data)
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1532
{
8081
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8073
diff changeset
  1533
	if (v->type != VEH_TRAIN) return NULL;
744
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1534
	return v;
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1535
}
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1536
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1537
static void DisableTrainCrossing(TileIndex tile)
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1538
{
3560
b2fcf1898eec (svn r4435) - Fix: an assertion triggered when trying to remove a bridge with the remove-tool (r4348 surfaced this). In CmdRemoveRoad tiletype was not checked for ownership. Intorudce IsLevelCrossingTile() which checks if a tile is a crossing without knowing the type. Suggested by peter1138 and Tron.
Darkvater
parents: 3546
diff changeset
  1539
	if (IsLevelCrossingTile(tile) &&
8081
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8073
diff changeset
  1540
			IsCrossingBarred(tile) &&
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8073
diff changeset
  1541
			VehicleFromPos(tile, NULL, &TestTrainOnCrossing) == NULL) { // empty?
3322
41b4d25b126d (svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents: 3315
diff changeset
  1542
		UnbarCrossing(tile);
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1543
		MarkTileDirtyByTile(tile);
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1544
	}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1545
}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1546
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1547
/**
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1548
 * Advances wagons for train reversing, needed for variable length wagons.
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1549
 * Needs to be called once before the train is reversed, and once after it.
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1550
 * @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
  1551
 * @param before Set to true for the call before reversing, false otherwise
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1552
 */
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1553
static void AdvanceWagons(Vehicle *v, bool before)
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1554
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1555
	Vehicle *base = 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
  1556
	Vehicle *first = base->Next();
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1557
	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
  1558
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1559
	while (length > 2) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1560
		/* find pairwise matching wagon
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1561
		 * start<>end, start+1<>end-1, ... */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1562
		Vehicle *last = first;
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
  1563
		for (uint i = length - 3; i > 0; i--) last = last->Next();
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1564
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1565
		int differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1566
		if (before) differential *= -1;
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1567
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1568
		if (differential > 0) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1569
			/* disconnect last car to make sure only this subset moves */
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
  1570
			Vehicle *tempnext = last->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
  1571
			last->SetNext(NULL);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1572
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1573
			for (int i = 0; i < differential; i++) TrainController(first, false);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1574
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
  1575
			last->SetNext(tempnext);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1576
		}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1577
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1578
		base = first;
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
  1579
		first = first->Next();
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1580
		length -= 2;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1581
	}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1582
}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1583
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2928
diff changeset
  1584
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1585
static void ReverseTrainDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1586
{
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1587
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) {
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1588
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1589
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1590
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1591
	/* Check if we were approaching a rail/road-crossing */
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1592
	{
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1593
		TileIndex tile = v->tile;
3163
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1594
		DiagDirection dir = DirToDiagDir(v->direction);
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1595
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1455
diff changeset
  1596
		/* Determine the diagonal direction in which we will exit this tile */
3163
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1597
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) {
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1598
			dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT);
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1599
		}
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1600
		/* Calculate next tile */
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4556
diff changeset
  1601
		tile += TileOffsByDiagDir(dir);
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1602
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1603
		/* Check if the train left a rail/road-crossing */
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1604
		DisableTrainCrossing(tile);
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1605
	}
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1606
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1607
	/* count number of vehicles */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1608
	int r = -1;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1609
	const 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
  1610
	do r++; while ((u = u->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1611
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1612
	AdvanceWagons(v, true);
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1613
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1614
	/* swap start<>end, start+1<>end-1, ... */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1615
	int l = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1616
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1617
		ReverseTrainSwapVeh(v, l++, r--);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1618
	} while (l <= r);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1619
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1620
	AdvanceWagons(v, false);
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1621
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1622
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) {
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1623
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  1624
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1625
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
  1626
	ClrBit(v->u.rail.flags, VRF_REVERSING);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1627
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1629
/** 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
  1630
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1631
 * @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
  1632
 * @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
  1633
 * @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
  1634
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1635
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
  1636
{
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1637
	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
  1638
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1639
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1640
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
  1641
	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
  1642
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1643
	if (p2) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1644
		/* 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
  1645
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1646
		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
  1647
			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
  1648
		}
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1649
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1650
		Vehicle *front = v->First();
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1651
		/* 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
  1652
		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
  1653
			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
  1654
		}
3257
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1655
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1656
		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
  1657
			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
  1658
			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
  1659
			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
  1660
		}
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1661
	} 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
  1662
		/* 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
  1663
		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
  1664
dbf829a8555b (svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents: 3256
diff changeset
  1665
		if (flags & DC_EXEC) {
3256
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1666
			if (_patches.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
  1667
				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
  1668
			} else {
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1669
				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
  1670
				SetLastSpeed(v, 0);
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1671
				ReverseTrainDirection(v);
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  1672
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1673
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1674
	}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1675
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1676
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1677
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1678
/** 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
  1679
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1680
 * @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
  1681
 * @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
  1682
 * @param p2 unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1683
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1684
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
  1685
{
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1686
	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
  1687
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1688
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1689
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
  1690
	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
  1691
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1692
	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
  1693
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1694
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1695
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1696
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
  1697
/** 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
  1698
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1699
 * @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
  1700
 * @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
  1701
 * param p2 various bitstuffed elements
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1702
 * - 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
  1703
 * - 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
  1704
 * - 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
  1705
 * @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
  1706
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1707
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
  1708
{
2635
7ed07303448d (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2634
diff changeset
  1709
	CargoID new_cid = GB(p2, 0, 8);
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1710
	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
  1711
	bool only_this = HasBit(p2, 16);
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1712
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  1713
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1714
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1715
	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
  1716
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
  1717
	if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR;
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
  1718
	if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
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
  1719
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
  1720
	/* Check cargo */
6316
d2b70e10f106 (svn r9259) -Codechange: NUM_CARGO isn't a valid cargo type...
peter1138
parents: 6303
diff changeset
  1721
	if (new_cid >= NUM_CARGO) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1722
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
  1723
	SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1724
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1725
	CommandCost cost;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1726
	uint num = 0;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1727
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1728
	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
  1729
		/* 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
  1730
		 * 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
  1731
		 * 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
  1732
		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
  1733
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
  1734
		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
  1735
			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
  1736
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1737
			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
  1738
				/* 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
  1739
				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
  1740
				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
  1741
				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
  1742
				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
  1743
				/* 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
  1744
				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
  1745
				/* 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
  1746
				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
  1747
				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
  1748
			}
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1749
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1750
			if (amount == CALLBACK_FAILED) { // callback failed or not used, use default
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1751
				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
  1752
				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
  1753
				/* 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
  1754
				 * 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
  1755
				 * 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
  1756
				 */
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1757
				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
  1758
				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
  1759
					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
  1760
					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
  1761
					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
  1762
					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
  1763
				}
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
  1764
				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
  1765
					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
  1766
					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
  1767
					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
  1768
					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
  1769
				}
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1770
			}
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1771
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1772
			if (amount != 0) {
4242
5738cbc9b2bd (svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents: 4198
diff changeset
  1773
				if (new_cid != v->cargo_type) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1774
					cost.AddCost(GetRefitCost(v->engine_type));
4242
5738cbc9b2bd (svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents: 4198
diff changeset
  1775
				}
5738cbc9b2bd (svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents: 4198
diff changeset
  1776
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1777
				num += amount;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1778
				if (flags & DC_EXEC) {
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
  1779
					v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0);
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1780
					v->cargo_type = new_cid;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1781
					v->cargo_cap = amount;
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1782
					v->cargo_subtype = new_subtype;
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1783
					InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1784
					InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1785
					RebuildVehicleLists();
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1786
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1787
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1788
		}
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
  1789
	} while ((v = v->Next()) != NULL && !only_this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1790
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
  1791
	_returned_refit_capacity = num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1792
4708
b2d7c0945664 (svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents: 4656
diff changeset
  1793
	/* Update the train's cached variables */
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1794
	if (flags & DC_EXEC) TrainConsistChanged(GetVehicle(p1)->First());
4708
b2d7c0945664 (svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents: 4656
diff changeset
  1795
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1796
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1797
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1798
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  1799
struct TrainFindDepotData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1800
	uint best_length;
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  1801
	TileIndex tile;
2475
efdcc705cb61 (svn r3001) s/Player*/const Player*/
tron
parents: 2464
diff changeset
  1802
	PlayerID 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
  1803
	/**
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1804
	 * true if reversing is necessary for the train to get to this depot
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1805
	 * 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
  1806
	 */
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
  1807
	bool reverse;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  1808
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1809
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1810
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
  1811
{
3269
62fb247bf94b (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  1812
	if (IsTileType(tile, MP_RAILWAY) &&
62fb247bf94b (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  1813
			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
  1814
			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
  1815
		/* 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
  1816
		tfdd->best_length = length / DIAG_FACTOR;
3269
62fb247bf94b (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  1817
		tfdd->tile = tile;
62fb247bf94b (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  1818
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1819
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1820
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1821
	return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1822
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1823
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1824
/** 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
  1825
 * 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
  1826
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v, int max_distance)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1827
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1828
	assert(!(v->vehstatus & VS_CRASHED));
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1829
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1830
	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
  1831
	tfdd.owner = v->owner;
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
  1832
	tfdd.best_length = (uint)-1;
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
  1833
	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
  1834
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1835
	TileIndex tile = v->tile;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1836
	if (IsTileDepotType(tile, TRANSPORT_RAIL)) {
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
  1837
		tfdd.tile = tile;
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
  1838
		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
  1839
		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
  1840
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1841
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1842
	if (_patches.yapf.rail_use_yapf) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1843
		bool found = YapfFindNearestRailDepotTwoWay(v, max_distance, NPF_INFINITE_PENALTY, &tfdd.tile, &tfdd.reverse);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1844
		tfdd.best_length = found ? max_distance / 2 : -1; // some fake distance or NOT_FOUND
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1845
	} else if (_patches.new_pathfinding_all) {
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
  1846
		Vehicle* last = GetLastVehicleInChain(v);
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1847
		Trackdir trackdir = GetVehicleTrackdir(v);
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1848
		Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1849
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1850
		assert(trackdir != INVALID_TRACKDIR);
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6644
diff changeset
  1851
		NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, last->tile, trackdir_rev, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes, NPF_INFINITE_PENALTY);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1852
		if (ftd.best_bird_dist == 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1853
			/* Found target */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1854
			tfdd.tile = ftd.node.tile;
1675
dd557a4d108d (svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents: 1615
diff changeset
  1855
			/* Our caller expects a number of tiles, so we just approximate that
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1856
			 * number by this. It might not be completely what we want, but it will
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1857
			 * work for now :-) We can possibly change this when the old pathfinder
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1858
			 * is removed. */
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
  1859
			tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH;
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
  1860
			if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) tfdd.reverse = true;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1861
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1862
	} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1863
		/* search in the forward direction first. */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1864
		DiagDirection i = DirToDiagDir(v->direction);
3163
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1865
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) {
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1866
			i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT);
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1867
		}
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
  1868
		NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &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
  1869
		if (tfdd.best_length == (uint)-1){
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
  1870
			tfdd.reverse = true;
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1871
			/* search in backwards direction */
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  1872
			i = ReverseDiagDir(DirToDiagDir(v->direction));
3163
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1873
			if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) {
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1874
				i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT);
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  1875
			}
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
  1876
			NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &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
  1877
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1878
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1879
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
  1880
	return tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1881
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1882
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1883
/** 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
  1884
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1885
 * @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
  1886
 * @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
  1887
 * @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
  1888
 * - 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
  1889
 * - 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
  1890
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1891
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
  1892
{
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1893
	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
  1894
		/* 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
  1895
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
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
  1896
		return SendAllVehiclesToDepot(VEH_TRAIN, flags, p2 & DEPOT_SERVICE, _current_player, (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
  1897
	}
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  1898
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1899
	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
  1900
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1901
	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
  1902
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
  1903
	if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR;
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1904
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1905
	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
1757
89f63a75ffd2 (svn r2261) - Fix: When crashed vehicles try to find a depot for servicing, openttd asserts.
matthijs
parents: 1752
diff changeset
  1906
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1907
	if (v->current_order.type == OT_GOTO_DEPOT) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1908
		if (!!(p2 & DEPOT_SERVICE) == HasBit(v->current_order.flags, OFB_HALT_IN_DEPOT)) {
4510
54d30a2372c3 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  1909
			/* We called with a different DEPOT_SERVICE setting.
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1910
			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1911
			 * Note: the if is (true for requesting service == true for ordered to stop in depot)          */
4510
54d30a2372c3 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  1912
			if (flags & DC_EXEC) {
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
  1913
				ClrBit(v->current_order.flags, OFB_PART_OF_ORDERS);
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
  1914
				ToggleBit(v->current_order.flags, OFB_HALT_IN_DEPOT);
4510
54d30a2372c3 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  1915
				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
54d30a2372c3 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  1916
			}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1917
			return CommandCost();
4510
54d30a2372c3 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  1918
		}
54d30a2372c3 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
  1919
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1920
		if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1921
		if (flags & DC_EXEC) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1922
			if (HasBit(v->current_order.flags, OFB_PART_OF_ORDERS)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1923
				v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1924
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1925
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1926
			v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1927
			v->current_order.flags = 0;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  1928
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1929
		}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1930
		return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1931
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1932
4526
6d48f07e829e (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
  1933
	/* check if at a standstill (not stopped only) in a depot
6d48f07e829e (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
  1934
	 * the check is down here to make it possible to alter stop/service for trains entering the depot */
6d48f07e829e (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
  1935
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL) && v->cur_speed == 0) return CMD_ERROR;
6d48f07e829e (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
  1936
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1937
	TrainFindDepotData tfdd = FindClosestTrainDepot(v, 0);
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1938
	if (tfdd.best_length == (uint)-1) return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1939
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1940
	if (flags & DC_EXEC) {
6502
8056fdc57aa9 (svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents: 6491
diff changeset
  1941
		if (v->current_order.type == OT_LOADING) v->LeaveStation();
8056fdc57aa9 (svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents: 6491
diff changeset
  1942
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
  1943
		v->dest_tile = tfdd.tile;
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1944
		v->current_order.type = OT_GOTO_DEPOT;
4412
cae52239a576 (svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents: 4406
diff changeset
  1945
		v->current_order.flags = OF_NON_STOP;
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
  1946
		if (!(p2 & DEPOT_SERVICE)) SetBit(v->current_order.flags, OFB_HALT_IN_DEPOT);
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1947
		v->current_order.dest = GetDepotByTile(tfdd.tile)->index;
5259
9d7de9a6b364 (svn r7389) -Fix: when forcing a vehicle to go to a depot, clear refit orders from current orders (caused refit error popup when not having no orders)
bjarni
parents: 5256
diff changeset
  1948
		v->current_order.refit_cargo = CT_INVALID;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  1949
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
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
  1950
		/* If there is no depot in front, reverse automatically */
6502
8056fdc57aa9 (svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents: 6491
diff changeset
  1951
		if (tfdd.reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1952
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1953
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1954
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1955
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1956
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1957
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  1958
void OnTick_Train()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1959
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1960
	_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
  1961
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1962
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1963
static const int8 _vehicle_smoke_pos[8] = {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1964
	1, 1, 1, 0, -1, -1, -1, 0
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1965
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1966
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
  1967
static void HandleLocomotiveSmokeCloud(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1968
{
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  1969
	bool sound = false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1970
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1971
	if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1972
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1973
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  1974
	const Vehicle* u = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1975
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1976
	do {
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
  1977
		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1978
		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
  1979
		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
  1980
		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
  1981
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1982
		/* 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
  1983
		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
  1984
				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
  1985
				v->vehstatus & VS_HIDDEN) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1986
			continue;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  1987
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1988
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1989
		/* No smoke in depots or tunnels */
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
  1990
		if (IsTileDepotType(v->tile, TRANSPORT_RAIL) || IsTunnelTile(v->tile)) continue;
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
  1991
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1992
		/* No sparks for electric vehicles on nonelectrified tracks */
6154
8f4c65bdf930 (svn r8899) -Fix
tron
parents: 6153
diff changeset
  1993
		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
  1994
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1995
		if (effect_type == 0) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  1996
			/* Use default effect type for engine class. */
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5769
diff changeset
  1997
			effect_type = rvi->engclass;
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1998
		} else {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1999
			effect_type--;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2000
		}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2001
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2002
		int x = _vehicle_smoke_pos[v->direction] * effect_offset;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2003
		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
  2004
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2005
		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
  2006
			x = -x;
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2007
			y = -y;
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2008
		}
3ada27c81b7f (svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents: 3242
diff changeset
  2009
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2010
		switch (effect_type) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2011
		case 0:
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2012
			/* steam smoke. */
2612
eccc835aaa44 (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  2013
			if (GB(v->tick_counter, 0, 4) == 0) {
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  2014
				CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE);
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2015
				sound = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2016
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2017
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2018
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2019
		case 1:
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2020
			/* diesel smoke */
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7932
diff changeset
  2021
			if (u->cur_speed <= 40 && Chance16(15, 128)) {
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
  2022
				CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2023
				sound = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2024
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2025
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2026
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2027
		case 2:
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2028
			/* blue spark */
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7932
diff changeset
  2029
			if (GB(v->tick_counter, 0, 2) == 0 && Chance16(1, 45)) {
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
  2030
				CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2031
				sound = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2032
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2034
		}
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
  2035
	} 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
  2036
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  2037
	if (sound) PlayVehicleSound(u, VSE_TRAIN_EFFECT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2038
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2039
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2040
static void TrainPlayLeaveStationSound(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2041
{
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2042
	static const SoundFx sfx[] = {
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2043
		SND_04_TRAIN,
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2044
		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
  2045
		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
  2046
		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
  2047
		SND_41_MAGLEV
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2048
	};
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2049
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2050
	if (PlayVehicleSound(v, VSE_START)) return;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2051
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
  2052
	EngineID engtype = v->engine_type;
6586
7491de82cefd (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium
parents: 6580
diff changeset
  2053
	SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2054
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2055
6593
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
  2056
void Train::PlayLeaveStationSound() const
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
  2057
{
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
  2058
	TrainPlayLeaveStationSound(this);
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
  2059
}
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
  2060
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2061
static bool CheckTrainStayInDepot(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2062
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2063
	/* 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
  2064
	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
  2065
		if (u->u.rail.track != TRACK_BIT_DEPOT || u->tile != v->tile) return false;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2066
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2067
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2068
	/* 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
  2069
	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
  2070
		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
  2071
		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
  2072
		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
  2073
	}
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
  2074
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2075
	if (v->u.rail.force_proceed == 0) {
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
  2076
		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
  2077
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2078
			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
  2079
		}
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2080
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2081
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2082
3172
ade284ef48ed (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3163
diff changeset
  2083
		if (UpdateSignalsOnSegment(v->tile, DirToDiagDir(v->direction))) {
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
  2084
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2085
			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
  2086
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2087
	}
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
  2088
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
  2089
	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
  2090
	InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2091
	TrainPlayLeaveStationSound(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2092
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2093
	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
  2094
	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
  2095
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2096
	v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2097
	v->cur_speed = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2098
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  2099
	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
  2100
	v->cur_image = v->GetImage(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2101
	VehiclePositionChanged(v);
3172
ade284ef48ed (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3163
diff changeset
  2102
	UpdateSignalsOnSegment(v->tile, DirToDiagDir(v->direction));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2103
	UpdateTrainAcceleration(v);
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4737
diff changeset
  2104
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2105
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2106
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2107
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2108
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2109
/* Check for station tiles */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  2110
struct TrainTrackFollowerData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2111
	TileIndex dest_coords;
1551
b1c7df6daa63 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  2112
	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
  2113
	uint best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2114
	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
  2115
	TrackdirByte best_track;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  2116
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2117
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2118
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
  2119
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2120
	/* heading for nowhere? */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2928
diff changeset
  2121
	if (ttfd->dest_coords == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2122
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2123
	/* 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
  2124
	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
  2125
				IsTileType(tile, MP_STATION) &&
3334
2999d2ddc0f9 (svn r4113) Moved all relevant map functions for stations to station_map.[ch]
celestar
parents: 3322
diff changeset
  2126
				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
  2127
				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
  2128
			)) {
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2129
		/* 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
  2130
		 * 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
  2131
		 * approximation of where the station is */
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2132
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2133
		/* 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
  2134
		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
  2135
		ttfd->best_bird_dist = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2136
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2137
	} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2138
		/* didn't find station, keep track of the best path so far. */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2139
		uint dist = DistanceManhattan(tile, ttfd->dest_coords);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2140
		if (dist < ttfd->best_bird_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2141
			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
  2142
			ttfd->best_track = track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2143
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2144
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2145
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2146
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2147
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
  2148
static void FillWithStationData(TrainTrackFollowerData* fd, const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2149
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2150
	fd->dest_coords = v->dest_tile;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2151
	if (v->current_order.type == OT_GOTO_STATION) {
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2152
		fd->station_index = v->current_order.dest;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2153
	} else {
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2154
		fd->station_index = INVALID_STATION;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2155
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2156
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2157
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2158
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
  2159
{{ 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
  2160
{{  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
  2161
{{  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
  2162
{{ 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
  2163
{{ 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
  2164
{{  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
  2165
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2166
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2167
static const uint32 _reachable_tracks[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2168
	0x10091009,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2169
	0x00160016,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2170
	0x05200520,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2171
	0x2A002A00,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2172
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2173
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2174
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
  2175
	{ 0, 9, 2, 9 }, ///< track 1
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2176
	{ 9, 1, 9, 3 }, ///< track 2
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2177
	{ 9, 0, 3, 9 }, ///< track upper
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2178
	{ 1, 9, 9, 2 }, ///< track lower
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2179
	{ 3, 2, 9, 9 }, ///< track left
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2180
	{ 9, 9, 1, 0 }, ///< track right
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2181
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2182
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2183
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
  2184
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2185
/* choose a track */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2186
static Track ChooseTrainTrack(Vehicle* v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2187
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2188
	Track best_track;
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2189
	/* pathfinders are able to tell that route was only 'guessed' */
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
  2190
	bool path_not_found = false;
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
  2191
2758
e962dd6c3ed4 (svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents: 2752
diff changeset
  2192
#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
  2193
	TIC()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2194
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2195
7833
ba402b153b79 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7802
diff changeset
  2196
	assert((tracks & ~TRACK_BIT_MASK) == 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2197
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2198
	/* quick return in case only one possible track is available */
7833
ba402b153b79 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 7802
diff changeset
  2199
	if (KillFirstBit(tracks) == TRACK_BIT_NONE) return FindFirstTrack(tracks);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2200
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2201
	if (_patches.yapf.rail_use_yapf) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2202
		Trackdir trackdir = YapfChooseRailTrack(v, tile, enterdir, tracks, &path_not_found);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2203
		if (trackdir != INVALID_TRACKDIR) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2204
			best_track = TrackdirToTrack(trackdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2205
		} else {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2206
			best_track = FindFirstTrack(tracks);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2207
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2208
	} else if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2209
		void* perf = NpfBeginInterval();
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2210
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2211
		NPFFindStationOrTileData fstd;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2212
		NPFFillWithOrderData(&fstd, v);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2213
		/* The enterdir for the new tile, is the exitdir for the old tile */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2214
		Trackdir trackdir = GetVehicleTrackdir(v);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2215
		assert(trackdir != 0xff);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2216
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6644
diff changeset
  2217
		NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, &fstd, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes);
1698
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2218
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2219
		if (ftd.best_trackdir == 0xff) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2220
			/* We are already at our target. Just do something
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2221
			 * @todo maybe display error?
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2222
			 * @todo: go straight ahead if possible? */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2223
			best_track = FindFirstTrack(tracks);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2224
		} else {
1698
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2225
			/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2226
			the direction we need to take to get there, if ftd.best_bird_dist is not 0,
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2227
			we did not find our target, but ftd.best_trackdir contains the direction leading
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2228
			to the tile closest to our target. */
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
  2229
			if (ftd.best_bird_dist != 0) path_not_found = true;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2230
			/* Discard enterdir information, making it a normal track */
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2231
			best_track = TrackdirToTrack(ftd.best_trackdir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2232
		}
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2233
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2234
		int time = NpfEndInterval(perf);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5316
diff changeset
  2235
		DEBUG(yapf, 4, "[NPFT] %d us - %d rounds - %d open - %d closed -- ", time, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2236
	} else {
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2237
		void* perf = NpfBeginInterval();
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2238
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2239
		TrainTrackFollowerData fd;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2240
		FillWithStationData(&fd, v);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2241
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2242
		/* New train pathfinding */
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2243
		fd.best_bird_dist = (uint)-1;
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2244
		fd.best_track_dist = (uint)-1;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2245
		fd.best_track = INVALID_TRACKDIR;
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2246
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4556
diff changeset
  2247
		NewTrainPathfind(tile - TileOffsByDiagDir(enterdir), v->dest_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
  2248
			v->u.rail.compatible_railtypes, enterdir, (NTPEnumProc*)NtpCallbFindStation, &fd);
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2249
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2250
		/* check whether the path was found or only 'guessed' */
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
  2251
		if (fd.best_bird_dist != 0) path_not_found = true;
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
  2252
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2253
		if (fd.best_track == 0xff) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2254
			/* blaha */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2255
			best_track = FindFirstTrack(tracks);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2256
		} else {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2257
			best_track = TrackdirToTrack(fd.best_track);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2258
		}
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2259
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2260
		int time = NpfEndInterval(perf);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5316
diff changeset
  2261
		DEBUG(yapf, 4, "[NTPT] %d us - %d rounds - %d open - %d closed -- ", time, 0, 0, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2262
	}
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2263
	/* handle "path not found" state */
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
  2264
	if (path_not_found) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2265
		/* PF didn't find the route */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2266
		if (!HasBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2267
			/* it is first time the problem occurred, set the "path not found" flag */
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
  2268
			SetBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION);
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2269
			/* and notify user about the event */
4996
6e704c18c8ba (svn r7003) -Fix: only the appropriate train owner can now see the "Train is lost" message (peter1138)
KUDr
parents: 4870
diff changeset
  2270
			if (_patches.lost_train_warn && v->owner == _local_player) {
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
  2271
				SetDParam(0, v->unitnumber);
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
  2272
				AddNewsItem(
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
  2273
					STR_TRAIN_IS_LOST,
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
  2274
					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
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
  2275
					v->index,
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
  2276
					0);
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
  2277
			}
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
  2278
		}
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
  2279
	} else {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2280
		/* route found, is the train marked with "path not found" flag? */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2281
		if (HasBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2282
			/* clear the flag as the PF's problem was solved */
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
  2283
			ClrBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION);
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2284
			/* can we also delete the "News" item somehow? */
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
  2285
		}
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
  2286
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2287
2758
e962dd6c3ed4 (svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents: 2752
diff changeset
  2288
#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
  2289
	TOC("PF time = ", 1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2290
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2291
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2292
	return best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2293
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2294
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2295
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2296
static bool CheckReverseTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2297
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2298
	if (_opt.diff.line_reverse_mode != 0 ||
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  2299
			v->u.rail.track == TRACK_BIT_DEPOT || v->u.rail.track == TRACK_BIT_WORMHOLE ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2300
			!(v->direction & 1))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2301
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2302
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2303
	TrainTrackFollowerData fd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2304
	FillWithStationData(&fd, v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2305
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2306
	uint reverse_best = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2307
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2308
	assert(v->u.rail.track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2309
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2310
	int i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][DirToDiagDir(v->direction)];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2311
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2312
	if (_patches.yapf.rail_use_yapf) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2313
		reverse_best = YapfCheckReverseTrain(v);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2314
	} else if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2315
		NPFFindStationOrTileData fstd;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2316
		NPFFoundTargetData ftd;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2317
		Trackdir trackdir, trackdir_rev;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2318
		Vehicle* last = GetLastVehicleInChain(v);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2319
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2320
		NPFFillWithOrderData(&fstd, v);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2321
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1698
diff changeset
  2322
		trackdir = GetVehicleTrackdir(v);
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2323
		trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2324
		assert(trackdir != 0xff);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2325
		assert(trackdir_rev != 0xff);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2326
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6644
diff changeset
  2327
		ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, last->tile, trackdir_rev, &fstd, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2328
		if (ftd.best_bird_dist != 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2329
			/* We didn't find anything, just keep on going straight ahead */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2330
			reverse_best = false;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2331
		} else {
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
  2332
			if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2333
				reverse_best = true;
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
  2334
			} else {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2335
				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
  2336
			}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2337
		}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2338
	} else {
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2339
		int best_track = -1;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2340
		uint reverse = 0;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2341
		uint best_bird_dist  = 0;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2342
		uint best_track_dist = 0;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2343
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  2344
		for (;;) {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2345
			fd.best_bird_dist = (uint)-1;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2346
			fd.best_track_dist = (uint)-1;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2347
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2348
			NewTrainPathfind(v->tile, v->dest_tile, v->u.rail.compatible_railtypes, (DiagDirection)(reverse ^ i), (NTPEnumProc*)NtpCallbFindStation, &fd);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2349
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2350
			if (best_track != -1) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2351
				if (best_bird_dist != 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2352
					if (fd.best_bird_dist != 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2353
						/* neither reached the destination, pick the one with the smallest bird dist */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2354
						if (fd.best_bird_dist > best_bird_dist) goto bad;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2355
						if (fd.best_bird_dist < best_bird_dist) goto good;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2356
					} else {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2357
						/* we found the destination for the first time */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2358
						goto good;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2359
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2360
				} else {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2361
					if (fd.best_bird_dist != 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2362
						/* didn't find destination, but we've found the destination previously */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2363
						goto bad;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2364
					} else {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2365
						/* both old & new reached the destination, compare track length */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2366
						if (fd.best_track_dist > best_track_dist) goto bad;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2367
						if (fd.best_track_dist < best_track_dist) goto good;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2368
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2369
				}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2370
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2371
				/* if we reach this position, there's two paths of equal value so far.
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2372
				 * pick one randomly. */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2373
				int r = GB(Random(), 0, 8);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2374
				if (_pick_track_table[i] == (v->direction & 3)) r += 80;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2375
				if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2376
				if (r <= 127) goto bad;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2377
			}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2378
good:;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2379
			best_track = i;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2380
			best_bird_dist = fd.best_bird_dist;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2381
			best_track_dist = fd.best_track_dist;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2382
			reverse_best = reverse;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2383
bad:;
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
  2384
			if (reverse != 0) break;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2385
			reverse = 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2386
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2387
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2388
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2389
	return reverse_best != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2390
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2391
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2392
static bool ProcessTrainOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2393
{
3005
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2394
	switch (v->current_order.type) {
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2395
		case OT_GOTO_DEPOT:
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2396
			if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return false;
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2397
			if ((v->current_order.flags & OF_SERVICE_IF_NEEDED) &&
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2398
					!VehicleNeedsService(v)) {
7070
2f4e1b2facc6 (svn r10335) -Fix (r10331): Increment the current order index *after* using it in UpdateVehicleTimetable.
maedhros
parents: 7052
diff changeset
  2399
				UpdateVehicleTimetable(v, true);
3005
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2400
				v->cur_order_index++;
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2401
			}
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2402
			break;
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2403
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2404
		case OT_LOADING:
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2999
diff changeset
  2405
		case OT_LEAVESTATION:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2406
			return false;
4351
c9799dd53eec (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
  2407
c9799dd53eec (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
  2408
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2409
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2410
7109
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2411
	/**
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2412
	 * Reversing because of order change is allowed only just after leaving a
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2413
	 * station (and the difficulty setting to allowed, of course)
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2414
	 * this can be detected because only after OT_LEAVESTATION, current_order
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2415
	 * will be reset to nothing. (That also happens if no order, but in that case
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2416
	 * it won't hit the point in code where may_reverse is checked)
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2417
	 */
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2418
	bool may_reverse = v->current_order.type == OT_NOTHING;
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2419
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2420
	/* check if we've reached the waypoint? */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2421
	if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) {
7070
2f4e1b2facc6 (svn r10335) -Fix (r10331): Increment the current order index *after* using it in UpdateVehicleTimetable.
maedhros
parents: 7052
diff changeset
  2422
		UpdateVehicleTimetable(v, true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2423
		v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2424
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2425
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2426
	/* check if we've reached a non-stop station while TTDPatch nonstop is enabled.. */
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2427
	if (_patches.new_nonstop &&
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2428
			v->current_order.flags & OF_NON_STOP &&
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2429
			IsTileType(v->tile, MP_STATION) &&
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2430
			v->current_order.dest == GetStationIndex(v->tile)) {
7070
2f4e1b2facc6 (svn r10335) -Fix (r10331): Increment the current order index *after* using it in UpdateVehicleTimetable.
maedhros
parents: 7052
diff changeset
  2431
		UpdateVehicleTimetable(v, true);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2432
		v->cur_order_index++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2433
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2434
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2435
	/* Get the current order */
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2436
	if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2437
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2438
	const Order *order = GetVehicleOrder(v, v->cur_order_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2439
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2440
	/* If no order, do nothing. */
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2441
	if (order == NULL) {
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
  2442
		v->current_order.Free();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2443
		v->dest_tile = 0;
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
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2447
	/* If it is unchanged, keep it. */
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2448
	if (order->type  == v->current_order.type &&
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2449
			order->flags == v->current_order.flags &&
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2450
			order->dest  == v->current_order.dest)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2451
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2452
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2453
	/* Otherwise set it, and determine the destination tile. */
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2454
	v->current_order = *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2455
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2456
	v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2457
3934
5aadf059954b (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2458
	InvalidateVehicleOrder(v);
5aadf059954b (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2459
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2460
	switch (order->type) {
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2461
		case OT_GOTO_STATION:
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2462
			if (order->dest == v->last_station_visited)
1266
eccd576e322f (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
  2463
				v->last_station_visited = INVALID_STATION;
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2464
			v->dest_tile = GetStation(order->dest)->xy;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2465
			break;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2466
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2467
		case OT_GOTO_DEPOT:
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2468
			v->dest_tile = GetDepot(order->dest)->xy;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2469
			break;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2470
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2471
		case OT_GOTO_WAYPOINT:
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2472
			v->dest_tile = GetWaypoint(order->dest)->xy;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2473
			break;
3934
5aadf059954b (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2474
5aadf059954b (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2475
		default:
5aadf059954b (svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents: 3933
diff changeset
  2476
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2477
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2478
7109
1e4193eb74ba (svn r10381) -Fix [FS#951]: skipping an order made train reverse immediatelly. Patch by boekabart.
rubidium
parents: 7096
diff changeset
  2479
	return may_reverse && CheckReverseTrain(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2480
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2481
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  2482
void Train::MarkDirty()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2483
{
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  2484
	Vehicle *v = this;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2485
	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
  2486
		v->cur_image = v->GetImage(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2487
		MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 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
  2488
	} while ((v = v->Next()) != NULL);
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  2489
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  2490
	/* 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
  2491
	TrainCargoChanged(this);
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  2492
	UpdateTrainAcceleration(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2493
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2494
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2495
static int UpdateTrainSpeed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2496
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2497
	uint accel;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2498
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2499
	if (v->vehstatus & VS_STOPPED || HasBit(v->u.rail.flags, VRF_REVERSING)) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2500
		if (_patches.realistic_acceleration) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2501
			accel = GetTrainAcceleration(v, AM_BRAKE) * 2;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2502
		} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2503
			accel = v->acceleration * -2;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2504
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2505
	} else {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2506
		if (_patches.realistic_acceleration) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2507
			accel = GetTrainAcceleration(v, AM_ACCEL);
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2508
		} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2509
			accel = v->acceleration;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2510
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2511
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2512
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2513
	uint spd = v->subspeed + accel * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2514
	v->subspeed = (byte)spd;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2515
	{
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2516
		int tempmax = v->max_speed;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2517
		if (v->cur_speed > v->max_speed)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2518
			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
  2519
		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
  2520
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2521
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2522
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2523
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2524
	spd += v->progress;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2525
	v->progress = (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2526
	return (spd >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2527
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2528
1551
b1c7df6daa63 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  2529
static void TrainEnterStation(Vehicle *v, StationID station)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2530
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2531
	v->last_station_visited = station;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2532
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2533
	/* check if a train ever visited this station before */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2534
	Station *st = GetStation(station);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2535
	if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2536
		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
  2537
		SetDParam(0, st->index);
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2538
		uint32 flags = v->owner == _local_player ?
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2539
			NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) :
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2540
			NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ARRIVAL_OTHER,  0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2541
		AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2542
			STR_8801_CITIZENS_CELEBRATE_FIRST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2543
			flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2544
			v->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
  2545
			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
  2546
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2547
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2548
6550
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2549
	v->BeginLoading();
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2550
	v->current_order.dest = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2551
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2552
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2553
static byte AfterSetTrainPos(Vehicle *v, bool new_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2554
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2555
	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
  2556
	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
  2557
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2558
	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
  2559
		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
  2560
		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
  2561
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
  2562
		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
  2563
			/* 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
  2564
			 * 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
  2565
			 * 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
  2566
			 * 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
  2567
			 * 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
  2568
			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
  2569
			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
  2570
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  2571
			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
  2572
80fd9b1cc0cd (svn r10317) -Fix [FS#786]: acceleration not calculated properly when a train goes up a hill between tunnels.
rubidium
parents: 7024
diff changeset
  2573
			/* 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
  2574
			 * 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
  2575
			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
  2576
				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
  2577
			}
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2578
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2579
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2580
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2581
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2582
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2583
	return old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2584
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2585
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  2586
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
  2587
	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
  2588
	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
  2589
	DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2590
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2591
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  2592
static Direction GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2593
{
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
  2594
	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
  2595
							TileX(new_tile) - TileX(old_tile) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2596
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2597
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2598
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2599
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  2600
static Direction GetNewVehicleDirection(const Vehicle *v, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2601
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2602
	uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2603
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2604
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2605
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2606
2436
7d5df545bd5d (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2425
diff changeset
  2607
static int GetDirectionToVehicle(const Vehicle *v, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2608
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2609
	byte offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2610
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2611
	x -= v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2612
	if (x >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2613
		offs = (x > 2) ? 0 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2614
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2615
		offs = (x < -2) ? 2 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2616
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2617
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2618
	y -= v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2619
	if (y >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2620
		offs += ((y > 2) ? 0 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2621
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2622
		offs += ((y < -2) ? 2 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2623
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2624
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2625
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2626
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2627
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2628
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2629
/* Check if the vehicle is compatible with the specified tile */
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2630
static bool CheckCompatibleRail(const Vehicle *v, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2631
{
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2632
	return
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2633
		IsTileOwner(tile, v->owner) && (
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  2634
			!IsFrontEngine(v) ||
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2635
			HasBit(v->u.rail.compatible_railtypes, GetRailType(tile))
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2636
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2637
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2638
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  2639
struct RailtypeSlowdownParams {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2640
	byte small_turn, large_turn;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2641
	byte z_up; // fraction to remove when moving up
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2642
	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
  2643
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2644
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
  2645
static const RailtypeSlowdownParams _railtype_slowdown[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2646
	// normal accel
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2647
	{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
  2648
	{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
  2649
	{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
  2650
	{0,       256 / 2, 256 / 4, 2}, ///< maglev
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2651
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2652
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2653
/** Modify the speed of the vehicle due to a turn */
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3154
diff changeset
  2654
static void AffectSpeedByDirChange(Vehicle* v, Direction new_dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2655
{
3158
696a6ca0bfa9 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2656
	if (_patches.realistic_acceleration) return;
696a6ca0bfa9 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2657
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2658
	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
  2659
	if (diff == DIRDIFF_SAME) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2660
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2661
	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
  2662
	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
  2663
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2664
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2665
/** Modify the speed of the vehicle due to a change in altitude */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2666
static void AffectSpeedByZChange(Vehicle *v, byte old_z)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2667
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2668
	if (old_z == v->z_pos || _patches.realistic_acceleration) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2669
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2670
	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
  2671
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2672
	if (old_z < v->z_pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2673
		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
  2674
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2675
		uint16 spd = v->cur_speed + rsp->z_down;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2676
		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
  2677
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2678
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2679
3172
ade284ef48ed (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3163
diff changeset
  2680
static const DiagDirection _otherside_signal_directions[] = {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2681
	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, INVALID_DIAGDIR, INVALID_DIAGDIR,
3172
ade284ef48ed (svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents: 3163
diff changeset
  2682
	DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2683
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2684
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
  2685
static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2686
{
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
  2687
	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
  2688
			GetRailTileType(tile) == RAIL_TILE_SIGNALS) {
3269
62fb247bf94b (svn r3981) More work for the rail accessing functions and enums
tron
parents: 3267
diff changeset
  2689
		uint i = FindFirstBit2x64(GetTrackBits(tile) * 0x101 & _reachable_tracks[dir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2690
		UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2691
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2692
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2693
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2694
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2695
static void SetVehicleCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2696
{
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
  2697
	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
  2698
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2699
	v->u.rail.crash_anim_pos++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2700
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2701
	Vehicle *u = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2702
	BEGIN_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2703
		v->vehstatus |= VS_CRASHED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2704
	END_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2705
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2706
	InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2707
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2708
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2709
static uint CountPassengersInTrain(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2710
{
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2711
	uint num = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2712
	BEGIN_ENUM_WAGONS(v)
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
  2713
		if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) num += v->cargo.Count();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2714
	END_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2715
	return num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2716
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2717
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
  2718
struct TrainCollideChecker {
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
  2719
	Vehicle *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
  2720
	const Vehicle *v_skip;
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
  2721
	uint num;
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
  2722
};
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
  2723
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
  2724
static void *FindTrainCollideEnum(Vehicle *v, void *data)
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
  2725
{
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
  2726
	TrainCollideChecker* tcc = (TrainCollideChecker*)data;
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
  2727
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
  2728
	if (v != 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
  2729
			v != tcc->v_skip &&
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
  2730
			v->type == VEH_TRAIN &&
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
  2731
			v->u.rail.track != TRACK_BIT_DEPOT &&
7923
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 7922
diff changeset
  2732
			abs(v->z_pos - tcc->v->z_pos) < 6 &&
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 7922
diff changeset
  2733
			abs(v->x_pos - tcc->v->x_pos) < 6 &&
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 7922
diff changeset
  2734
			abs(v->y_pos - tcc->v->y_pos) < 6 ) {
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
  2735
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2736
		Vehicle *coll = v->First();
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
  2737
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
  2738
		/* it can't collide with its own wagons */
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
  2739
		if (tcc->v == 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
  2740
			(tcc->v->u.rail.track == TRACK_BIT_WORMHOLE && (tcc->v->direction & 2) != (v->direction & 2)))
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
  2741
			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
  2742
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
  2743
		/* two drivers + passengers killed in train tcc->v (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
  2744
		if (!(tcc->v->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
  2745
			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
  2746
			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
  2747
		}
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
  2748
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
  2749
		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
  2750
			/* 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
  2751
			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
  2752
			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
  2753
		}
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
  2754
	}
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
  2755
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
  2756
	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
  2757
}
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
  2758
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2759
/**
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2760
 * 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
  2761
 * 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
  2762
 * 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
  2763
 * plays a sound.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2764
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2765
static void CheckTrainCollision(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2766
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2767
	/* can't collide in depot */
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  2768
	if (v->u.rail.track == TRACK_BIT_DEPOT) return;
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  2769
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  2770
	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
  2771
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2772
	TrainCollideChecker tcc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2773
	tcc.v = 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
  2774
	tcc.v_skip = v->Next();
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
  2775
	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
  2776
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
  2777
	/* 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
  2778
	if (v->u.rail.track == TRACK_BIT_WORMHOLE) {
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  2779
		VehicleFromPos(v->tile, &tcc, FindTrainCollideEnum);
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  2780
		if (IsBridgeTile(v->tile)) {
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  2781
			VehicleFromPos(GetOtherBridgeEnd(v->tile), &tcc, FindTrainCollideEnum);
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  2782
		} else {
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  2783
			VehicleFromPos(GetOtherTunnelEnd(v->tile), &tcc, FindTrainCollideEnum);
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  2784
		}
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  2785
	} else {
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  2786
		VehicleFromPosXY(v->x_pos, v->y_pos, &tcc, FindTrainCollideEnum);
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7334
diff changeset
  2787
	}
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
  2788
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
  2789
	/* any dead -> no crash */
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
  2790
	if (tcc.num == 0) return;
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
  2791
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
  2792
	SetDParam(0, tcc.num);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2793
	AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL,
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2794
		NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ACCIDENT, 0),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2795
		v->index,
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2796
		0
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2797
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2798
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2799
	ModifyStationRatingAround(v->tile, v->owner, -160, 30);
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2800
	SndPlayVehicleFx(SND_13_BIG_CRASH, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2801
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2802
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2803
static void *CheckVehicleAtSignal(Vehicle *v, void *data)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2804
{
8081
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8073
diff changeset
  2805
	Direction dir = *(Direction*)data;
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8073
diff changeset
  2806
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8073
diff changeset
  2807
	if (v->type == VEH_TRAIN && IsFrontEngine(v)) {
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8073
diff changeset
  2808
		DirDiff diff = ChangeDirDiff(DirDifference(v->direction, dir), DIRDIFF_90RIGHT);
3158
696a6ca0bfa9 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2809
696a6ca0bfa9 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  2810
		if (diff == DIRDIFF_90RIGHT || (v->cur_speed <= 5 && diff <= DIRDIFF_REVERSE)) return v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2811
	}
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
  2812
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2813
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2814
5252
d4b78248de25 (svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents: 5215
diff changeset
  2815
static void TrainController(Vehicle *v, bool update_image)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2816
{
1961
60efcaa14311 (svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents: 1942
diff changeset
  2817
	Vehicle *prev;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2818
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2819
	/* For every vehicle after and including the given vehicle */
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2820
	for (prev = v->Previous(); v != NULL; 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
  2821
		DiagDirection enterdir = DIAGDIR_BEGIN;
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
  2822
		bool update_signals = false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2823
		BeginVehicleMove(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2824
6153
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
  2825
		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  2826
		if (v->u.rail.track != TRACK_BIT_WORMHOLE) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2827
			/* Not inside tunnel */
6152
6f9fbee77a47 (svn r8896) -Fix
tron
parents: 6151
diff changeset
  2828
			if (gp.old_tile == gp.new_tile) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2829
				/* Staying in the old tile */
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  2830
				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
  2831
					/* Inside depot */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2832
					gp.x = v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2833
					gp.y = v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2834
				} 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
  2835
					/* Not inside depot */
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2836
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
  2837
					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
  2838
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2839
					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
  2840
					if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2841
						goto invalid_rail;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2842
					}
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2843
					if (HasBit(r, VETS_ENTERED_STATION)) {
5991
ec2eebfe86de (svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents: 5944
diff changeset
  2844
						TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2845
						return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2846
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2847
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2848
					if (v->current_order.type == 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
  2849
						v->current_order.Free();
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2850
						InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2851
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2852
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2853
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2854
				/* A new tile is about to be entered. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2855
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2856
				/* Determine what direction we're entering the new tile from */
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2857
				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
  2858
				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
  2859
				assert(IsValidDiagDirection(enterdir));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2860
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2861
				/* 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
  2862
				 * away the bits that aren't reachable. */
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6644
diff changeset
  2863
				uint32 ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL, 0) & _reachable_tracks[enterdir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2864
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2865
				/* Combine the from & to directions.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2866
				 * Now, the lower byte contains the track status, and the byte at bit 16 contains
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2867
				 * the signal status. */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2868
				uint32 tracks = ts | (ts >> 8);
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2869
				TrackBits bits = (TrackBits)(tracks & TRACK_BIT_MASK);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  2870
				if ((_patches.new_pathfinding_all || _patches.yapf.rail_use_yapf) && _patches.forbid_90_deg && prev == NULL) {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2871
					/* We allow wagons to make 90 deg turns, because forbid_90_deg
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2872
					 * 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
  2873
					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
  2874
				}
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
  2875
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
  2876
				if (bits == TRACK_BIT_NONE) goto invalid_rail;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2877
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2878
				/* 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
  2879
				 * 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
  2880
				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
  2881
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2882
				TrackBits chosen_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2883
				if (prev == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2884
					/* 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
  2885
					 * available tracks to choose */
6155
38e464212f4a (svn r8900) -Fix
tron
parents: 6154
diff changeset
  2886
					chosen_track = TrackToTrackBits(ChooseTrainTrack(v, gp.new_tile, enterdir, bits));
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2887
					assert(chosen_track & tracks);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2888
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2889
					/* Check if it's a red signal and that force proceed is not clicked. */
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2890
					if ((tracks >> 16) & chosen_track && v->u.rail.force_proceed == 0) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2891
						/* In front of a red signal
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  2892
						 * find the first set bit in ts. need to do it in 2 steps, since
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2893
						 * FIND_FIRST_BIT only handles 6 bits at a time. */
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2894
						Trackdir i = FindFirstTrackdir((TrackdirBits)(uint16)ts);
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2895
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2896
						if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) {
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2897
							v->cur_speed = 0;
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2898
							v->subspeed = 0;
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2899
							v->progress = 255 - 100;
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2900
							if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20) return;
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2901
						} else if (HasSignalOnTrackdir(gp.new_tile, i)) {
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2902
							v->cur_speed = 0;
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2903
							v->subspeed = 0;
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6490
diff changeset
  2904
							v->progress = 255 - 10;
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2905
							if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) {
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2906
								TileIndex o_tile = gp.new_tile + TileOffsByDiagDir(enterdir);
8081
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8073
diff changeset
  2907
								Direction rdir = ReverseDir(dir);
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2908
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2909
								/* check if a train is waiting on the other side */
8081
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8073
diff changeset
  2910
								if (VehicleFromPos(o_tile, &rdir, &CheckVehicleAtSignal) == NULL) return;
6151
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2911
							}
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2912
						}
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2913
						goto reverse_train_direction;
0986553bca91 (svn r8894) -Fix
tron
parents: 6150
diff changeset
  2914
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2915
				} 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
  2916
					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
  2917
							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
  2918
							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
  2919
							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
  2920
							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
  2921
					};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2922
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2923
					/* 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
  2924
					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
  2925
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2926
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
  2927
				/* 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
  2928
				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
  2929
						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
  2930
						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
  2931
						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
  2932
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2933
				/* 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
  2934
				const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2935
				gp.x = (gp.x & ~0xF) | b[0];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2936
				gp.y = (gp.y & ~0xF) | b[1];
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2937
				Direction chosen_dir = (Direction)b[2];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2938
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2939
				/* Call the landscape function and tell it that the vehicle entered the tile */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2940
				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
  2941
				if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2942
					goto invalid_rail;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2943
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2944
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
  2945
				if (IsLevelCrossingTile(v->tile) && v->Next() == NULL) {
3657
eb28d97d3d18 (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  2946
					UnbarCrossing(v->tile);
eb28d97d3d18 (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  2947
					MarkTileDirtyByTile(v->tile);
eb28d97d3d18 (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  2948
				}
eb28d97d3d18 (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  2949
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  2950
				if (IsFrontEngine(v)) v->load_unload_time_rem = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2951
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2952
				if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2953
					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
  2954
6154
8f4c65bdf930 (svn r8899) -Fix
tron
parents: 6153
diff changeset
  2955
					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
  2956
						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
  2957
					}
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
  2958
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2959
					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
  2960
					assert(v->u.rail.track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2961
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2962
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
  2963
				/* 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
  2964
				 * has been updated by AfterSetTrainPos() */
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
  2965
				update_signals = true;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2966
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
  2967
				if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2968
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2969
				v->direction = chosen_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2970
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2971
		} 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
  2972
			/* In tunnel or on a bridge */
6141
bd1637f172b4 (svn r8884) -Fix
tron
parents: 6136
diff changeset
  2973
			if (!(v->vehstatus & VS_HIDDEN)) {
bd1637f172b4 (svn r8884) -Fix
tron
parents: 6136
diff changeset
  2974
				v->cur_speed =
bd1637f172b4 (svn r8884) -Fix
tron
parents: 6136
diff changeset
  2975
					min(v->cur_speed, GetBridge(GetBridgeType(v->tile))->speed);
bd1637f172b4 (svn r8884) -Fix
tron
parents: 6136
diff changeset
  2976
			}
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  2977
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2978
			if (!(IsTunnelTile(gp.new_tile) || IsBridgeTile(gp.new_tile)) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2979
				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
  2980
				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
  2981
				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
  2982
				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
  2983
				continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2984
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2985
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2986
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2987
		/* update image of train, as well as delta XY */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2988
		Direction newdir = GetNewVehicleDirection(v, gp.x, gp.y);
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  2989
		v->UpdateDeltaXY(newdir);
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
  2990
		if (update_image) v->cur_image = v->GetImage(newdir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2991
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2992
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2993
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2994
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2995
		/* update the Z position of the vehicle */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  2996
		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
  2997
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2998
		if (prev == NULL) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2999
			/* This is the first vehicle in the train */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3000
			AffectSpeedByZChange(v, old_z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3001
		}
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
  3002
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
  3003
		if (update_signals) {
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
  3004
			if (IsFrontEngine(v)) TrainMovedChangeSignals(gp.new_tile, enterdir);
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
  3005
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
  3006
			/* 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
  3007
			 * (above) or the last vehicle moves. */
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
  3008
			if (v->Next() == NULL) TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
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
  3009
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3010
	}
1438
98d01c6873f7 (svn r1942) Fix r1938
tron
parents: 1434
diff changeset
  3011
	return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3012
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3013
invalid_rail:
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3014
	/* We've reached end of line?? */
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3015
	if (prev != NULL) error("!Disconnecting train");
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3016
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3017
reverse_train_direction:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3018
	v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3019
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3020
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3021
	ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3022
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3023
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3024
/**
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3025
 * 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
  3026
 * 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
  3027
 * 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
  3028
 * 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
  3029
 * @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
  3030
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3031
static void DeleteLastWagon(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3032
{
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3033
	/* 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
  3034
	 * *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
  3035
	 * one which will physicially be removed */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3036
	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
  3037
	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
  3038
	u->SetNext(NULL);
0
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
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3041
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
  3042
	RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3043
	InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3044
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3045
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3046
	EndVehicleMove(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
  3047
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7371
diff changeset
  3048
	delete v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3049
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3050
	if (v->u.rail.track != TRACK_BIT_DEPOT && v->u.rail.track != TRACK_BIT_WORMHOLE)
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3051
		SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(v->u.rail.track));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3052
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3053
	/* Check if the wagon was on a road/rail-crossing and disable it if no
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3054
	 * others are on it */
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  3055
	DisableTrainCrossing(v->tile);
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  3056
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
  3057
	if (v->u.rail.track == TRACK_BIT_WORMHOLE) { // inside a tunnel / bridge
5a254e1c34e7 (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz
parents: 8072
diff changeset
  3058
		TileIndex endtile = IsTunnel(v->tile) ? GetOtherTunnelEnd(v->tile) : GetOtherBridgeEnd(v->tile);
5a254e1c34e7 (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz
parents: 8072
diff changeset
  3059
5a254e1c34e7 (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz
parents: 8072
diff changeset
  3060
		if (GetVehicleTunnelBridge(v->tile, endtile) != NULL) return; // tunnel / bridge is busy
5a254e1c34e7 (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz
parents: 8072
diff changeset
  3061
8088
92fca5b09665 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8083
diff changeset
  3062
		DiagDirection dir = GetTunnelBridgeDirection(v->tile);
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
  3063
5a254e1c34e7 (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz
parents: 8072
diff changeset
  3064
		/* v->direction is "random", so it cannot be used to determine the direction of the track */
5a254e1c34e7 (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz
parents: 8072
diff changeset
  3065
		UpdateSignalsOnSegment(v->tile, dir);
5a254e1c34e7 (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz
parents: 8072
diff changeset
  3066
		UpdateSignalsOnSegment(endtile, ReverseDiagDir(dir));
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  3067
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3068
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3069
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3070
static void ChangeTrainDirRandomly(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3071
{
3160
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3072
	static const DirDiff delta[] = {
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3073
		DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  3074
	};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3075
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3076
	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
  3077
		/* 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
  3078
		if (!(v->vehstatus & VS_HIDDEN)) {
3977
513433ebd092 (svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents: 3956
diff changeset
  3079
			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
  3080
			BeginVehicleMove(v);
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6554
diff changeset
  3081
			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
  3082
			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
  3083
			/* Refrain from updating the z position of the vehicle when on
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  3084
			   a bridge, because AfterSetTrainPos will put the vehicle under
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  3085
			   the bridge in that case */
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3086
			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
  3087
		}
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
  3088
	} while ((v = v->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3089
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3090
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3091
static void HandleCrashedTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3092
{
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
  3093
	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
  3094
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3095
	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
  3096
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3097
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3098
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3099
	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
  3100
	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
  3101
		int index = (r * 10 >> 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3102
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3103
		Vehicle *u = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3104
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3105
			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
  3106
				r = Random();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3107
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3108
				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
  3109
					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
  3110
					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
  3111
					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
  3112
					EV_EXPLOSION_SMALL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3113
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3114
			}
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
  3115
		} while ((u = u->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3116
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3117
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
  3118
	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
  3119
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
  3120
	if (state >= 4440 && !(v->tick_counter&0x1F)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3121
		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
  3122
		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
  3123
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3124
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3125
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3126
static void HandleBrokenTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3127
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3128
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3129
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3130
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3131
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3132
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3133
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3134
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3135
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3136
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3137
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
  3138
		if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
6357
f0f5e7d1713c (svn r9400) -Codechange: Use some more representative enum names for landscape types.
belugas
parents: 6334
diff changeset
  3139
			SndPlayVehicleFx((_opt.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
  3140
				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
  3141
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3142
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3143
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3144
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
7334
ed9a43cf642a (svn r10697) -Codechange: give a more sensible names to some of the unkX variables.
rubidium
parents: 7266
diff changeset
  3145
			if (u != NULL) u->u.special.animation_state = v->breakdown_delay * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3146
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3147
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3148
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3149
	if (!(v->tick_counter & 3)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3150
		if (!--v->breakdown_delay) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3151
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3152
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3153
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3154
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3155
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3156
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3157
static const byte _breakdown_speeds[16] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3158
	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
  3159
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3160
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3161
static bool TrainCheckIfLineEnds(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3162
{
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3163
	int t = v->breakdown_ctr;
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
  3164
	if (t > 1) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3165
		v->vehstatus |= VS_TRAIN_SLOWING;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3166
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3167
		uint16 break_speed = _breakdown_speeds[GB(~t, 4, 4)];
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
  3168
		if (break_speed < v->cur_speed) v->cur_speed = break_speed;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3169
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3170
		v->vehstatus &= ~VS_TRAIN_SLOWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3171
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3172
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3173
	if (v->u.rail.track == TRACK_BIT_WORMHOLE) return true; // exit if inside a tunnel
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5992
diff changeset
  3174
	if (v->u.rail.track == TRACK_BIT_DEPOT) return true; // exit if inside a depot
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3175
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3176
	TileIndex tile = v->tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3177
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  3178
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
8088
92fca5b09665 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8083
diff changeset
  3179
		DiagDirection dir = GetTunnelBridgeDirection(tile);
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  3180
		if (DiagDirToDir(dir) == v->direction) return true;
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents: 3153
diff changeset
  3181
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3182
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3183
	// depot?
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3184
	/* XXX -- When enabled, this makes it possible to crash trains of others
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3185
	     (by building a depot right against a station) */
4632
1141148a11bf (svn r6496) -Codechange: removed direct map access in train_cmd.c (Rubidium)
glx
parents: 4574
diff changeset
  3186
/*	if (IsTileType(tile, MP_RAILWAY) && GetRailTileType(tile) == RAIL_TILE_DEPOT_WAYPOINT)
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3187
		return true;*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3188
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3189
	/* Determine the non-diagonal direction in which we will exit this tile */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3190
	DiagDirection dir = DirToDiagDir(v->direction);
3163
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  3191
	if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) {
14e1eba3591d (svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents: 3161
diff changeset
  3192
		dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3193
	}
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3194
	/* Calculate next tile */
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4556
diff changeset
  3195
	tile += TileOffsByDiagDir(dir);
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3196
	// determine the track status on the next tile.
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6644
diff changeset
  3197
	uint32 ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0) & _reachable_tracks[dir];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3198
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3199
	/* Calc position within the current tile ?? */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3200
	uint x = v->x_pos & 0xF;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3201
	uint y = v->y_pos & 0xF;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3202
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3203
	switch (v->direction) {
3186
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3204
		case DIR_N : x = ~x + ~y + 24; break;
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3205
		case DIR_NW: x = y;            /* FALLTHROUGH */
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3206
		case DIR_NE: x = ~x + 16;      break;
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3207
		case DIR_E : x = ~x + y + 8;   break;
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3208
		case DIR_SE: x = y;            break;
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3209
		case DIR_S : x = x + y - 8;    break;
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3185
diff changeset
  3210
		case DIR_W : x = ~y + x + 8;   break;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  3211
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3212
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3213
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
  3214
	if (GB(ts, 0, 16) != 0) {
2874
3dcc067a7946 (svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents: 2855
diff changeset
  3215
		/* If we approach a rail-piece which we can't enter, or the back of a depot, don't enter it! */
3645
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
  3216
		if (x + 4 >= TILE_SIZE &&
2874
3dcc067a7946 (svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents: 2855
diff changeset
  3217
				(!CheckCompatibleRail(v, tile) ||
3dcc067a7946 (svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents: 2855
diff changeset
  3218
				(IsTileDepotType(tile, TRANSPORT_RAIL) &&
3185
a9d0cdff7b84 (svn r3831) Add and use GetRailDepotDirection()
tron
parents: 3184
diff changeset
  3219
				GetRailDepotDirection(tile) == dir))) {
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3220
			v->cur_speed = 0;
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3221
			ReverseTrainDirection(v);
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3222
			return false;
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3223
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3224
		if ((ts &= (ts >> 16)) == 0) {
8058
5d32eaa21927 (svn r11619) -Fix [FS#1531]: do not make crossing red behind depot the train is entering
smatz
parents: 8007
diff changeset
  3225
			/* make a rail/road crossing red
5d32eaa21927 (svn r11619) -Fix [FS#1531]: do not make crossing red behind depot the train is entering
smatz
parents: 8007
diff changeset
  3226
			 * do not make crossing red behind depot the train is entering */
8062
eb133d21f363 (svn r11623) -Fix [FS#1533] (r11619): assert when train is going over two crossings next to each other
smatz
parents: 8058
diff changeset
  3227
			if (IsLevelCrossingTile(tile) && (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || GetRailDepotDirection(v->tile) == dir)) {
3322
41b4d25b126d (svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents: 3315
diff changeset
  3228
				if (!IsCrossingBarred(tile)) {
41b4d25b126d (svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents: 3315
diff changeset
  3229
					BarCrossing(tile);
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  3230
					SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3231
					MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3232
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3233
			}
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3234
			return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3235
		}
3645
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3636
diff changeset
  3236
	} else if (x + 4 >= TILE_SIZE) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3237
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3238
		ReverseTrainDirection(v);
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3239
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3240
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3241
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3242
	/* slow down */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3243
	v->vehstatus |= VS_TRAIN_SLOWING;
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3244
	uint16 break_speed = _breakdown_speeds[x & 0xF];
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
  3245
	if (!(v->direction & 1)) break_speed >>= 1;
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
  3246
	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
  3247
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3248
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3249
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3250
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3251
static void TrainLocoHandler(Vehicle *v, bool mode)
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
	/* 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
  3254
	if (v->vehstatus & VS_CRASHED) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3255
		if (!mode) HandleCrashedTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3256
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3257
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3258
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
  3259
	if (v->u.rail.force_proceed != 0) v->u.rail.force_proceed--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3260
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3261
	/* train is broken down? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3262
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3263
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3264
			HandleBrokenTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3265
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3266
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3267
		v->breakdown_ctr--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3268
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3269
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  3270
	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
  3271
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3272
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3273
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3274
	/* 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
  3275
	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
  3276
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3277
	if (ProcessTrainOrder(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3278
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3279
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3280
		v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3281
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3282
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3283
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3284
6594
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6593
diff changeset
  3285
	v->HandleLoading(mode);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3286
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
  3287
	if (v->current_order.type == OT_LOADING) return;
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
  3288
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
  3289
	if (CheckTrainStayInDepot(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3290
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3291
	if (!mode) HandleLocomotiveSmokeCloud(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3292
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3293
	int j = UpdateTrainSpeed(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3294
	if (j == 0) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3295
		/* if the vehicle has speed 0, update the last_speed field. */
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
  3296
		if (v->cur_speed != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3297
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3298
		TrainCheckIfLineEnds(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3299
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3300
		do {
5252
d4b78248de25 (svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents: 5215
diff changeset
  3301
			TrainController(v, true);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  3302
			CheckTrainCollision(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3303
			if (v->cur_speed <= 0x100)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3304
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3305
		} while (--j != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3306
	}
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
	SetLastSpeed(v, v->cur_speed);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3309
}
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
7488
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3312
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3313
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
  3314
{
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3315
	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
  3316
	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
  3317
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3318
	do {
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3319
		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
  3320
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3321
		byte cost_factor = GetVehicleProperty(v, 0x0D, rvi->running_cost_base);
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3322
		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
  3323
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3324
		cost += cost_factor * _price.running_rail[rvi->running_cost_class];
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3325
	} 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
  3326
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3327
	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
  3328
}
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3329
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3330
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  3331
void Train::Tick()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3332
{
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  3333
	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
  3334
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  3335
	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
  3336
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  3337
	if (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
  3338
		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
  3339
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  3340
		TrainLocoHandler(this, false);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3341
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3342
		/* 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
  3343
		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
  3344
			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
  3345
	} 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
  3346
		/* 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
  3347
		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
  3348
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3349
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3350
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  3351
#define MAX_ACCEPTABLE_DEPOT_DIST 16
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  3352
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3353
static void CheckIfTrainNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3354
{
7502
f821f134ec5a (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium
parents: 7497
diff changeset
  3355
	if (_patches.servint_trains == 0 || !VehicleNeedsService(v)) return;
f821f134ec5a (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium
parents: 7497
diff changeset
  3356
	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
  3357
		VehicleServiceInDepot(v);
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3358
		return;
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3359
	}
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  3360
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3361
	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
  3362
	/* Only go to the depot if it is not too far out of our way. */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  3363
	if (tfdd.best_length == (uint)-1 || tfdd.best_length > MAX_ACCEPTABLE_DEPOT_DIST) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3364
		if (v->current_order.type == 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
  3365
			/* 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
  3366
			 * 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
  3367
			 * schedule? */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3368
			v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3369
			v->current_order.flags = 0;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  3370
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3371
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3372
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3373
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3374
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3375
	const Depot* depot = GetDepotByTile(tfdd.tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3376
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3377
	if (v->current_order.type == OT_GOTO_DEPOT &&
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  3378
			v->current_order.dest != 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
  3379
			!Chance16(3, 16)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3380
		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
  3381
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3382
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3383
	v->current_order.type = OT_GOTO_DEPOT;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3384
	v->current_order.flags = OF_NON_STOP;
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  3385
	v->current_order.dest = depot->index;
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
  3386
	v->dest_tile = tfdd.tile;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  3387
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3390
void OnNewDay_Train(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3391
{
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
  3392
	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3393
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  3394
	if (IsFrontEngine(v)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3395
		CheckVehicleBreakdown(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3396
		AgeVehicle(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3397
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3398
		CheckIfTrainNeedsService(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3399
3140
5555cc95043a (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  3400
		CheckOrders(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3401
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3402
		/* update destination */
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3403
		if (v->current_order.type == OT_GOTO_STATION) {
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3404
			TileIndex tile = GetStation(v->current_order.dest)->train_tile;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3405
			if (tile != 0) v->dest_tile = tile;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  3406
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3407
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3408
		if ((v->vehstatus & VS_STOPPED) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3409
			/* running costs */
7488
6f51ddf4c225 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium
parents: 7486
diff changeset
  3410
			CommandCost cost(v->GetRunningCost() / 364);
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  3411
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  3412
			v->profit_this_year -= cost.GetCost() >> 8;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3413
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3414
			SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3415
			SubtractMoneyFromPlayerFract(v->owner, cost);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3416
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3417
			InvalidateWindow(WC_VEHICLE_DETAILS, v->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
  3418
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3419
		}
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
  3420
	} else if (IsTrainEngine(v)) {
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
  3421
		/* Also age engines that aren't front engines */
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
  3422
		AgeVehicle(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3423
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3424
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3425
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  3426
void TrainsYearlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3427
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3428
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3429
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3430
	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
  3431
		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
  3432
			/* show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list) */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3433
			if (_patches.train_income_warn && v->owner == _local_player && v->age >= 730 && v->profit_this_year < 0) {
7002
1bf6a62b0fcb (svn r10258) -Codechange: as we are now using int64 all over the place, it's better to use int64 variables in the string generating too instead of packing them into two int32s.
rubidium
parents: 6991
diff changeset
  3434
				SetDParam(1, v->profit_this_year);
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  3435
				SetDParam(0, v->unitnumber);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3436
				AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3437
					STR_TRAIN_IS_UNPROFITABLE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3438
					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3439
					v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3440
					0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3441
			}
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
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3444
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3445
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3446
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3447
	}
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
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  3451
void InitializeTrains()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3452
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3453
	_age_cargo_skip_counter = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3454
}
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3455
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3456
/*
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3457
 * 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
  3458
 * 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
  3459
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  3460
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
  3461
{
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3462
	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
  3463
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3464
	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
  3465
		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
  3466
			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
  3467
		}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3468
	}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3469
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3470
	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
  3471
		if (v->type == VEH_TRAIN && IsFrontEngine(v)) {
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3472
			Vehicle *u = v;
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3473
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3474
			BEGIN_ENUM_WAGONS(u) {
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3475
				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
  3476
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3477
				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
  3478
					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
  3479
						/* 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
  3480
						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
  3481
						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
  3482
					}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3483
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3484
					Vehicle *w;
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
  3485
					for (w = u->Next(); w != NULL && (w->engine_type != u->engine_type || w->u.rail.other_multiheaded_part != NULL); w = GetNextVehicle(w));
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3486
					if (w != NULL) {
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3487
						/* we found a car to partner with this engine. Now we will make sure it face the right way */
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3488
						if (IsTrainEngine(w)) {
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3489
							ClearTrainEngine(w);
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3490
							w->spritenum++;
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3491
						}
6150
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3492
						w->u.rail.other_multiheaded_part = u;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3493
						u->u.rail.other_multiheaded_part = w;
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3494
					} else {
98af28cf9665 (svn r8893) -Fix
tron
parents: 6141
diff changeset
  3495
						/* 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
  3496
						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
  3497
					}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3498
				}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3499
			} END_ENUM_WAGONS(u)
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3500
		}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3501
	}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3502
}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3503
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6407
diff changeset
  3504
/**
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3505
 *  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
  3506
 *  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
  3507
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  3508
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
  3509
{
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3510
	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
  3511
	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
  3512
		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
  3513
			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
  3514
		}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3515
	}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3516
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3517
	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
  3518
		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
  3519
			if (HasBit(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) {
2855
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3520
				Vehicle *u = v;
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3521
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
  3522
				BEGIN_ENUM_WAGONS(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
  3523
					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
  3524
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
  3525
					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
  3526
					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
  3527
						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
  3528
							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
  3529
							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
  3530
							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
  3531
							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
  3532
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
  3533
						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
  3534
							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
  3535
							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
  3536
							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
  3537
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
  3538
						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
  3539
							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
  3540
							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
  3541
								// 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
  3542
								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
  3543
								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
  3544
							}
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
  3545
							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
  3546
								// 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
  3547
								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
  3548
								break;
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3549
							}
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
  3550
							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
  3551
							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
  3552
							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
  3553
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
  3554
						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
  3555
							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
  3556
							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
  3557
							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
  3558
							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
  3559
						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
  3560
					}
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
  3561
				} END_ENUM_WAGONS(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
  3562
			}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3563
		}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3564
	}
56c39efde08a (svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents: 2854
diff changeset
  3565
}